Variables

[ LiB ]

Variables

Variables are intrinsic to almost every program written. A variable is just that: "variable." This means that the value of a variable can change. For example, say you were running a program which uses a high score that is stored in a variable. When the high score is beaten, the high score variable changes to reflect the new score.

Declaring Variables

Variables are very easy to use because they can be used as regular numbers. However, unlike numbers , variables must first be declared. When a variable is declared, the program knows that the variable exists, and you can use it in your program.

There are three types of variables in BASIC: integer variables, floating point variables, and string variables. See Table 2.2 for a description of the types of variables.

Table 2.2. Description of Variable Types

Parameter

Description

integer%

Fixed-point variables with no decimal places

float#

Floating-point variables with decimal places allowed

string$

A text string


NOTE

When variables are created, they are automatically assumed to be integers, or whole numbers in other words.Therefore, the percent sign on all integer variables is unnecessary, and from now on, they will be mostly omitted from the code.

Each type of variable is defined in a similar way. Simply type the name of the variable you want to define followed by the type symbol (%, #, or $). For example,

 highscore% = 100 pi# = 3.14159 myname$ = "Maneesh Sethi" 

Using Variables

You are now ready to write a few programs using variables. These programs should demonstrate a few important points about variables.

 ;demo02-02.bb  Adds two cool numbers favnum = 314 coolnum = 13 Print "I like " + favnum + " And I like " + coolnum Print "These numbers added together are " + (favnum + coolnum) WaitKey 

The output is shown in Figure 2.4.

Figure 2.4. The demo02-02.bb program.


Well, this is certainly interesting. Let's check it out. First, a comment is written to describe the program. This is good practice and should be used on most programs. Next , we initialize two variables: favnum and coolnum . Then, we call the Print function. The string variable begins with the static text "I like" and then displays favnum . To display favnum , we use the concatenation operator ( + ). The concatenation operator links separate strings together; in this case, it displays the variable favnum . It finishes out the first Print statement by displaying "And I like" + the variable coolnum .

The next Print statement displays "These numbers added together are" and shows 327, which is equal to 314 + 13. However, try removing the parentheses around favnum and coolnum , like in Figure 2.5. A strange answer comes up when these parentheses are removed: 31413!

Figure 2.5. Demo02-02.bb without parentheses.


The reason for this strange answer is that without the parentheses, the addition operator ( + ) is interpreted as the concatenation operator due to the context in which it is used. Because there are no parentheses, the operator simply adds the string "13" to the end of the string "314" and displays it as a string rather than an integer. The only way to fix this problem is to use parentheses.

Here is an example using only string variables.

 ;demo03-03.bb  adds strings together string1$ = "I " string2$ = "like " string3$ = "programming!" completestring$ = string1$ + string2$ + string3$ Print completestring$ WaitKey 

In this program, a set of single words are created and joined together in the completestring$ variable using the concatenation operator. As you can see in Figure 2.6, "I" + "like " + "programming!" becomes "I like programming!"

Figure 2.6. The demo02-03.bb program.


[ LiB ]


Game Programming for Teens
Game Programming for Teens
ISBN: 1598635182
EAN: 2147483647
Year: 2004
Pages: 94
Authors: Maneesh Sethi

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net