|
This chapter discusses an important topic in programming, "variables." Please read this section thoroughly. A variable is a piece of data kept in the computer's memory (RAM). The location of a variable in RAM is called the "address." ![]() How a variable is stored in RAM The following program prints the variable X to the screen:
This next program sets X to 15, and then prints the variable:
print X
In the above example, the number 15 was stored in the computer's RAM at a certain memory address. Then the PRINT command accessed (or looked at) that address when it printed "15" to the screen. ![]() (NOTE: The memory address of X is not necessarily 1000000)
As in the programs above, a variable is accessed by calling its name. Variable names can have a combination of letters and numbers. The following are valid variables:
num VALUE xYz abc123 Also, you can use multiple variables in your program.
Y = 101 Z = 79 PRINT X PRINT Y PRINT Z
101 79 ![]() (NOTE: The memory addresses of these variables are not necessarily as specified) Expressions If you pass an expression to a variable, the expression is evaluated and the variable is set to that value.
PRINT x
You can also use variables as expressions.
time = 2 distance = rate * time PRINT distance
Plus, you can have both variables and numbers in an expression.
Y = X * 7 PRINT Y
Strings If you add a dollar sign ($) to the end of a variable, the variable is a string.
PRINT X$
If you try to set a string to a non-string variable, an error occurs.
A string can be added to the end of an existing variable string.
X$ = X$ + "World" PRINT X$
You can also add variable strings together.
b$ = "String2" c$ = "String3" d$ = a$ + b$ + c$ PRINT d$
Feel free to distribute this tutorial, upload it to your website, link to it from your site, etc. http://www.geocities.com/progsharehouse/qbtutor Mirror: http://development.freeservers.com/qbtutor |