domingo, 6 de abril de 2014

Variables

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
END
When 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
END
In 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
So after this statement has executed a section of memory named NUM holds 23.5:
NUM
23.5
After the first statement has executed, the second statement executes:
PRINT "The variable contains", NUM
The 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 NUM for a number.
  • It prints out the number it found.
So on the monitor you see:
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
END
The first statement does several things:
  1. Memory is reserved for the variable VALUE
  2. A number is calculated: 2 + 3 = 5
  3. The number 5 is stored in the variable.
The 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
So after this statement has executed a section of memory named AGE holds 23:
AGE
23
The first time a particular variable is used in a program, a section of main memory will be reserved for it.

QUESTION 5:

Can the value in AGE 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:
  1. It prints "You have lived more than"
  2. It looks in the variable AGE for a value to use.
  3. It multiplies that value by 365 but DOES NOT change the contents of AGE .
  4. It prints out the result of the calculation.
So on the monitor you see:
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:
MILE
5
The 5 will stay in 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     5
Notice 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

  1. A variable is a small amount of computer memory that has been given a name.
  2. 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.
  3. You can't use a word that is already used for something else. (You can't use PRINT as a name vor a variable.)
  4. 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
  5. Different names mean different places in memory.
  6. The last character is part of the name, (so SUM% and SUM are different variables.)
  7. If two names differ only in upper and lower case characters, they are really the same name.
So far we have been using variables that hold floating point numbers (those with a possible decimal point like the numbers on a hand calculator.) This type of variable is the most useful. These variables can hold numbers like 1.3, -45.78, 0.001, and can also hold numbers without a fractional part like 1.0, -23.0, 94.0 and others.

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