![]() |
It's time to add variables to your programs. You have probably used variables in Algebra problems. In Algebra, you often write an equation that contains both numbers and letters. The letters are variables, and their value can vary. The concept of using variables in computer programs is similar. The big difference is in programming, variables store values when the program is run.
A variable is a symbolic representation for a value that is stored in the computer's memory. By using the variable, the value can be recalled then processed and used within the same computer program.
Closet | Computer Memory | |
---|---|---|
Boxes | = | Memory address |
Labels | = | Variable Names |
Contents (shoes, book, hat) | = | Values (numbers, words) |
Storing values in variables is similar to storing items in your closet in an orderly way. Visualize a closet with a set of boxes. In order to remember what is in those boxes (a pair of shoes, a hat, a book), a label is placed on them. When you want to wear your hot pink high-tops or reread the book assigned for your Language Arts class, you look at the labels on the boxes. The contents of the boxes can also change. As you out grow a pair of shoes, it is replaced with a new pair.
Let’s look at the above example, but this time the closet is the computer's memory. It is divided into many "boxes". The labels are variables and the contents of the boxes are the values. Variables are assigned values. Those values do not have to stay the same and may change; hence the name variable.
Using boxes to keep your closet organized is obviously better than leaving your stuff scattered all over the room. The same is true with variables. Using variables to store values keeps your program organized.
The concept of "memory storage" is beyond the scope of this course, but the previous analogy and the examples just given are really all you need to understand at this point.
Computers process two types of data: numeric and alphanumeric (or string). You have written several programs in your Lessons that processed numeric data. Soon, you will write a program containing alphanumeric values.
Numeric variables can be assigned numeric data. Numeric data is simply a number that can be used in calculations. Examples of numeric data are numbers such as your age, weight, a distance, or salary. The computer can perform calculations using the values of numeric variables.
String variables can store alphanumeric data. Alphanumeric data can be anything, such as letters, symbols, words, and numbers. Some examples would include a person's name, a street address, a telephone number or social security number. Notice some of those values contain numbers but they also contain other characters such as dashes or letters. The computer cannot perform math with string variables.
Check your knowledge on identifying values(data) as type Alphanumeric or Numeric.
A text-version of the self-check is also available.
You will learn more about the differences in these two types variables and values as you progress as a programmer, but the distinction is sufficient for now.
When labeling boxes, you use words that help identify their contents. The same will be true with choosing the names for your variables. If a box contains a pair of hot pink high tops, then the label may say Shoes. When you step into your closet you will have no problem finding your box of shoes. As you out grow your shoes, they can be replaced with a new pair thus changing the content of your box.
As you choose variable names, you will want them to be just as meaningful. Pick names easy to remember as you work on your program. Trust me, when a program grows to be over 1000 lines long, you’ll want easy to remember variable names! Attempting to remember the purpose of a variable named p or n$ can be made much easier by using more descript variable names such as percent1 and name1$. Using variable names with meaning, is a "good programming practice".
Check your knowledge on identifying variables as either type Numeric or String.
A text-version of the self-check is also available.
Look at the example variable names. Can you guess their purpose by their names?
Did you notice that some of the variable names ended with a dollar sign ($)? Did that seem a bit odd? A dollar sign at the end the name tells the computer the variable is a string. Remember, there are two types of variables and values we will work with in QBASIC: numeric and string. When you pick variable names, you have to tell the computer which type of variable it is. Numeric variables are easy, just pick a descriptive word and you are done. For string variables, add the $ to the end of the name. The dollar sign will tell the computer the variable is a string.
Want a trick for remembering when to add a $ to a variable name?
String variable names need the $ at the end. The word STRING starts with an S. A dollar sign looks like an S. So just remember String means to add a $!
QBasic imposes five rules to follow when you create a variable name. You must know (and follow) these rules to successfully write programs.
Note: BASE is a keyword in QBASIC. It has a defined purpose just like LET, CLS, and PRINT. So what can you do if you really want to use "base" as the variable name? Did you notice many of the variable names have a number? Well, by adding the number 1 to the end of base, we’ve created a valid variable name: base1. There is another reason for adding the number but we will cover that in a later lesson. For now, practice adding a number such as 1 to your variable names.
Check your knowledge on categorizing variable names as valid and invalid.
A text-version of the self-check is also available.
Now let's see how variables are assigned values in a program.
You have learned about the two types of variables and how to choose meaningful variable names. Now let's learn how to assign values to them with assignment statements. The QBASIC keyword for assignment statements is LET. With LET, you can assign values to variables. Variables will be assigned values in the INPUT and PROCESSING sections of your program. Let's look at simple assignment statements that you might find in the INPUT section of your program.
The table below shows example LET statements. In assignment statements, the variable receiving the value is to the left of the equal sign and the value is on the right. That may seem a bit backwards from the way you are used to setting up equations. Notice string values must be placed inside quotes.
INPUT Section | |||||
---|---|---|---|---|---|
Keyword | Variable name | Equal sign | Value | Notice the following: | |
LET | hours1 | = | 35 | The numeric variable hours1 is assigned the numeric value 35 | |
LET | wage1 | = | 6.25 | The numeric variable wage1 is assigned the numeric value 6.25. | |
LET | student1$ | = | "Joe College" | The string variable student1$ is assigned the alphanumeric value Joe College. | |
LET | phone1$ | = | "555-123-4567" | The string variable phone1$ is assigned the alphanumeric value 555-123-4567. |
Now that you've assigned values to variables, let's do something with them. Variables should be used in the PROCESSING and OUTPUT sections of your programs. Once you've assigned a value to a variable in the INPUT section, the variable, not the value, should be used throughout the rest of the program.
PROCESSING Section |
---|
LET total1 = wage1 * hours1 |
Notice the following:
-The expression is on the right of the equal sign (=).
-The numeric variable total1 to the left of the equal sign is assigned the results of the equation.
-The variables wage1 and hours1 were assigned values in the INPUT section of the program.
-When the computer encounters the variables wage1 and hours1, it looks up the values in the computer's memory. The values are used to solve the equation.
-String variables can not be used in equations.
You may be wondering how to PRINT with variables. Let's look at an example.
OUTPUT Section |
---|
REM Output PRINT student1$; " worked "; hours1; " hours last week and earned $"; total1 PRINT "You may call him Saturday morning at "; phone1$ END |
When you run the program above, the OUTPUT will look like: |
|
Notice the following: When the computer encounters a variable in a PRINT statement, the value of the variable is displayed on the screen, not the name of the variable. The user never sees the name of the variable. The literals are printed exactly as they appear inside the quotes.
Let's create a simple program to help demonstrate assignment statements. We will assign string and numeric values to variables. Next, perform calculations using those numeric variables and store the result in a numeric variable. Finally, display the results on the output screen.
The problem:
Calculate Mr. Blue's wage when given the number of hours worked (23) and his hourly rate of pay ($5.25). The output should be in sentence form stating his name and wage.
Below is the pseudocode and flowchart for the wage calculation algorithm. The pseudocode is a plain English description of the algorithm. The flowchart is a pictorial representation. The variables are shown in blue.
PSEUDOCODE * Use NOTEPAD | FLOWCHART |
INPUT (assign data) Mr. Blue is the employee (employee1$) His wage is $5.25 per hour and this past week, (rate1) He worked for 23 hours. (hours1) PROCESSING (calculations) Calculate the total pay for the employee by multiplying the hours worked by the hourly rate(wage1) Formula: wage1 = rate1 * hours1 OUTPUT (print the results) Print a sentence stating the employee's name and wage for the week. Be sure to use both string and numeric variables. |
![]() |
From the algorithm above, we can code the program. The results are below.
REM Wage Calculator REM Joe College REM June 1, 2007 ' COLOR 15,1 CLS ' REM INPUT - assign data LET employee1$ = "Mr. Blue" LET rate1 = 5.25 LET hours1 = 23 ' REM PROCESSING - calculate wage LET wage1 = rate1 * hours1 ' REM OUTPUT - print results to output screen PRINT "The wage for "; employee1$; " is $"; wage1; "." ' END |
Look over the program. Can you predict the output? What do you think will appear on the output screen?
Now type in the program exactly as it is written. Save your program as wages.bas. As you type the program, notice the variable names. Also note how values are assigned. Remember to include all the punctuation marks in the PRINT statement.
Run the program. Did it work as predicted? The output should be:
The wage for Mr. Blue is $120.75. |
Let's dissect and analyze the program to understand it clearly. Notice the following:
This program is a model for the programs you will be writing throughout this course. Study it carefully, very carefully.
Let's Get Ready to Program!
As you get ready to start writing programs with assignment statements, please remember the following. A computer program should be easy for someone who didn't design it to read and understand. Write your programs in organized blocks of code with REM statements indicating the purpose of each section. Use meaningful variable names. For example, "wage1" is a better variable name than just "w". "Wage1" is more descriptive of what the data represents and is easier to remember.
YOUR ASSIGNMENT Modify the wage.bas program in the following four ways.
Save and RUN your program. Write down the final sentence exactly as it appears on the output screen. You will need it when taking the Quiz on this lesson. This lesson is packed with important information. Review the objectives carefully and make sure you know the information covered in this lesson. When you are ready, take the Quiz for Assignment 02.05. |
A QBasic ReviewBefore moving on to the next assignment, be sure that you can do the following:
|
![]() |
When you are ready to take the quiz, go to the Quizzes area and select Assignment 02.05 Knowledge Chip. Once you have completed the assignment and submitted it, please move on to the next assignment |
![]() |
![]() |