Saturday, November 12, 2011

Literals, Constants, and Variables

Literals can be used in output either alone or in combination with constants, variables, or other literals.

String Literals
The following line of BASIC code uses the PRINT command to print the string literal "Hello World!" to the screen:
PRINT "Hello World!"

Numeric Literals
The following line of BASIC code uses the PRINT command to print the sum of 2 and 3 to the screen:
PRINT 2 + 3

Numeric and string literals may be used together. The following code uses one PRINT command to print a string literal and a numeric literal on the same line:
PRINT "The sum of 2 and 3 is" 2 + 3

GEEK NOTE: Literals are technically "literal constants". The term "literal constant" doesn't make any sense though, since they are NOT stored in memory, they CANNOT be called anywhere else in a program, and are therefore NOT constants! They are literal and temporary. Not constant or permanent. For this reason I call them "literals".

In QuickBASIC there are two types of constants. The first type of constant is called "literal constant" and is what I just described as a "literal" (scroll up and see the note in yellow). The second type of constant is what I think of as a true constant. The technical term is "symbolic constant". For the sake of simplicity I will refer to these as "constants".

Constants are values you can call from other parts of your program. Constants remain constant. They do not vary. You cannot change them somewhere else in your program. You can, however, refer to them in other parts of your program.

Real-world constants include your birth date, the number of hours in a day (24), and how many quarters make up a dollar (4).

Constants are declared with the CONST statement. The syntax for the CONST statement is:
CONST name = expression [,name = expression]...

If you were writing a program which would use pi, you could either write out 3.14159 every time you need to use pi, or you could declare pi as a constant:
CONST pi = 3.14159

Using constants in this way not only cuts down on errors (what happens if you type the number wrong just once?), it also increases the readability of your programs.

It should be noted that in the example just given, QuickBASIC made pi a single precision number. This is a default in QuickBASIC. You may want to get in the habit of using a data type suffix for single and double precision numbers. You cannot use any other numeric type declaration characters on constants.

As mentioned before, you cannot change the value of a constant. If you declare pi as in the sample above and then later in the program write the following statement...
pi = pi +1

... the compiler will generate an error.

Variables have a name, data type, and a value. You can call and change variables throughout your program.

Real-world variables include your age, the number of hours you sleep in a day (0 to 24), and how many quarters are in your pocket.

Variable Names
Variable names begin with a letter and may contain up to 40 letters or numbers. You may not use QB reserved words as variable names. It is recommended that you use a data type suffix for all variables.

Variable Types
QB has five standard data types. Here's a handy table that shows these data types and the type declaration character (or "suffix") for each type.
Data TypeSuffix
"Type Declaration Character"
Highest ValueLowest Value
Long Integer
(absolute value)
(absolute value)
(absolute value)
(absolute value)
STRING$32,767 Characters0 Characters

 You can assign one of these data types to a variable by using type declaration characters like so:

This gives our program two string variables and one integer variable.

You could also assign a data type to a variable as part of a DIM statement:
DIM FirstName as STRING
DIM LastName as STRING

Do not use type declaration characters in DIM statements unless you are using arrays.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.