Home Previous section Next section

Variables Star


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

How a variable is stored in RAM


The following program prints the variable X to the screen:
    print X
Since the variable hasn't been assigned a number, the value of the variable is 0. So, the output of the program is:
    0

This next program sets X to 15, and then prints the variable:
    X = 15

    print X
This time, the output is:
    15

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.


How the X variable is stored in RAM

(NOTE: The memory address of X is not necessarily 1000000)



ADVANCED TIP: Although you don't normally need to, you can find the actual memory address of a variable (X, for example) by using the VARSEG and VARPTR commands.

PRINT (VARSEG(X) * 65536) + VARPTR(X)

(For more information, see Memory.)


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:
    Y

    num

    VALUE

    xYz

    abc123


Also, you can use multiple variables in your program.
    X = 82
    Y = 101
    Z = 79

    PRINT X
    PRINT Y
    PRINT Z
Output:
    82
    101
    79

How the X, Y, and Z variables are stored in RAM

(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.
    x = 500 + (10 * 7)

    PRINT x
Output:
    570

You can also use variables as expressions.
    rate = 50
    time = 2

    distance = rate * time

    PRINT distance
Output:
    100

Plus, you can have both variables and numbers in an expression.
    X = 100
    Y = X * 7

    PRINT Y
Output:
    700

TIP: The following increases X by 1:

X = X + 1



Strings

If you add a dollar sign ($) to the end of a variable, the variable is a string.
    X$ = "Hello World!"

    PRINT X$
Output:
    Hello World!

If you try to set a string to a non-string variable, an error occurs.
    X = "Hello World!"
The QBasic interpreter says "Type mismatch" when you try to run the above program.


A string can be added to the end of an existing variable string.
    X$ = "Hello"
    X$ = X$ + "World"

    PRINT X$
Output:
    HelloWorld

You can also add variable strings together.
    a$ = "String1"
    b$ = "String2"
    c$ = "String3"

    d$ = a$ + b$ + c$

    PRINT d$
Output:
    String1String2String3

Next section


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