Variables
Modern computers have a large amount of main memory (also called RAM). This memory is used for many things. When you run a QBasic program, the statements of the program are stored in main memory. The data for the program also can be stored in main memory.A variable in QBasic is a small amount of computer memory that has been given a name. You (the programmer) think of the name you want to use. The QBasic system will use a section of main memory for that name.
A variable is like a small box that holds a value. The value in the variable can change (that is why it is called a variable). Here is a program that uses a variable:
' Program that uses a variable ' LET NUM = 23.5 PRINT "The variable contains", NUM ENDWhen this program runs, the value 23.5 is placed in the variable
NUM.
Then the following is written to the monitor:
The variable contains 23.5
QUESTION:
What do you think the following program will write to the monitor?
' Program with a variable ' LET NUM = 53 PRINT "The variable contains", NUM END
The LET Statement
Look at the (first) program again:' Program that uses a variable ' LET NUM = 23.5 PRINT "The variable contains", NUM ENDIn this program,
NUM is a variable.
The programmer chose the name NUM .
When the program runs,
several things happen when the LET statement executes:
- Memory is reserved for the variable
NUM - The number 23.5 is stored in the variable
NUM holds 23.5:
| 23.5 |
PRINT "The variable contains", NUMThe variable
NUM already exists,
so no more memory is reserved for it.
The PRINT statement does several things:
- It prints "The variable contains"
- It looks in the variable
NUMfor a number. - It prints out the number it found.
The variable contains 23.5
QUESTION 3:
What do you think the following program will write to the monitor?' Program with a variable ' LET VALUE = 2 + 3 PRINT "The result is", VALUE END
Saving a Result
Look at the new program again:' Program with a variable ' LET VALUE = 2 + 3 PRINT "The result is", VALUE ENDThe first statement does several things:
- Memory is reserved for the variable
VALUE - A number is calculated: 2 + 3 = 5
- The number 5 is stored in the variable.
LET statement can be used with a variable to
save the result of arithmetic.
The value saved in the variable will stay there
until another statement changes it, or until the
program stops running.
QUESTION 4:
What do you think the following program will write to the monitor?' Saving a result in a variable ' LET SUM = 1 + 2 + 3 PRINT "The sum is", SUM END
Arithmetic with Variables
Look at the following program. Remember the idea of sequential execution: Unless directed otherwise, a QBasic program runs by starting with the first statement and executing the statements in sequence.' Arithmetic with Variables
'
LET AGE = 23
PRINT "You have lived more than", AGE * 365, " days"
END
The first statement,
LET AGE = 23
does two things:
- Memory is reserved for the variable AGE
- The value 23 is stored in the variable
AGE holds 23:
| 23 |
QUESTION 5:
Can the value inAGE be used in a calculation?Calculation
After the first statement executes, the second statement executes.PRINT "You have lived more than", AGE*365, " days"
This statement does several things:
- It prints "You have lived more than"
- It looks in the variable
AGEfor a value to use. - It multiplies that value by 365 but DOES NOT change
the contents of
AGE. - It prints out the result of the calculation.
You have lived more than 8395 days
QUESTION 6:
What do you think the following program will write to the monitor?' Pounds to ounces ' LET POUNDS = 3 PRINT "The number of ounces is ", POUNDS * 16 PRINT "The number of pounds is ", POUNDS END
Using the Contents of a Variable
The value stored in a variable can be used as many times as you want. Here is another program that uses a variable many times without changing the contents.' 5280 feet per Mile
' 1 yard per 3 feet
' 12 inches per foot
'
LET MILE = 5
PRINT "Number of Miles", MILE
PRINT "Number of Yards", MILE * 5280 / 3
PRINT "Number of Feet", MILE * 5280
PRINT "Number of Inches", MILE * 5280 * 12
PRINT "Number of Miles", MILE
END
The first statement finds memory for the variable MILE and puts
the value 5 into it:
| 5 |
MILE until you change it
(with a second LET
statement, for instance).
Using the variable in an arithmetic expression does not
change it.
The following statements will execute one after the other, in order.
The program will print out:
Number of Miles 5 Number of Yards 8800 Number of Feet 26400 Number of Inches 316800 Number of Miles 5Notice that the value 5 in
MILE does not change, so the
first PRINT statement and the last
PRINT statement write the same
thing to the monitor.
QUESTION 7:
What do you think the following program will write to the monitor?' Hours of Boring Lectures
'
LET CLASSES = 4
PRINT "Hours per Week", CLASSES * 3
PRINT "Hours per Semester", CLASSES * 3 * 15
PRINT "Ho Hum..."
END
Names for Variables
Remember that a variable is a small amount of computer memory that has been given a name. So far you have seen several names for variables:NUM POUNDS
VALUE MILE
SUM CLASSES
AGE
Names for variables are single words that
the programmer picks.
The names don't have to be real words, but it
helps in understanding the program if they are.
Look over the following rules.
(Just read through them once or twice;
you don't have to memorize them.)
Rules for Variable Names
- A variable is a small amount of computer memory that has been given a name.
- The name is:
- up to 40 characters long,
- the first character must be A-Z, a-z,
- the rest of the characters must be A-Z, a-z, 0-9, or "."
- no spaces are allowed inside of a name.
- You can't use a word that is already used for something else. (You can't use PRINT as a name vor a variable.)
- The last character of the name tells what type of data
the memory holds:
- SUM% holds an integer (no decimal point)
- SUM& holds a potentially very big integer
- SUM holds a floating point number (can have a decimal point)
- SUM# holds a potentially very big floating point number
- SUM$ holds a string of characters
- Different names mean different places in memory.
- The last character is part of the name, (so SUM% and SUM are different variables.)
- If two names differ only in upper and lower case characters, they are really the same name.
QUESTION 8:
Which of the following are OK names to use for a variable that will hold a floating point number?SUM GRAND TOTAL MyValue 16Candles SUM23 YEAR%
No hay comentarios:
Publicar un comentario