Module 2 - Knowledge
Assignment 2.05

Objectives: After studying the Module 2 Knowledge Chip, you will be able to:

  1. Distinguish between numeric and alphanumeric data.
  2. Use an analogy to explain how a computer stores data.
  3. Define the following terms: variable, variable name, numeric variable, string variable and assignment statement.
  4. Use the key word LET to assign data to variables.
  5. Correctly use the rules for naming variables.
  6. Modify a simple program using numeric and alphanumeric data.


YOUR LESSON

Introduction

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.

Variables

ClosetComputer 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.

Types of Variables - Numeric and String

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.

qb2_5-2.jpg & qb2_5_1.jpg

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.

Self Check Icon

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.

Choosing Variable Names

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.

Self Check Icon

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.

HINT:

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 $!

Rules for Naming Variables

QBasic imposes five rules to follow when you create a variable name. You must know (and follow) these rules to successfully write programs.

  1. A variable name must begin with a letter.
  2. A variable name may contain any combination of letters from A to Z (upper and lower case) and digits from 0 to 9.
  3. A variable name cannot contain a space.
  4. A variable name can be as long as 40 characters, but names longer than 10 or 15 characters become unwieldy in a program.
  5. A variable name must be unique. It may not be the same as a constant, a keyword, or any other existing variable. (You will learn about constants in Module 3.)


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.

Self Check Icon

A text-version of the self-check is also available.

Now let's see how variables are assigned values in a program.

Assigning Values to Variables

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.

Using Variables in PROCESSING

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.

Using Variables in OUTPUT

You may be wondering how to PRINT with variables. Let's look at an example.

OUTPUT Section

REM Output
COLOR 14, 1
CLS

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:


Joe College worked 30 hours last week and earned $ 218.75
You may call him Saturday morning at 555-123-4567

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.

Creating a Program

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 NOTEPADFLOWCHART

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.

flowchart (2_5_n.jpg)

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:

  1. This program is well structured. The REM statements and the apostrophes (') clearly break the program into three sections: INPUT, PROCESSING, OUTPUT
  2. The REM statements also document what each section does.
  3. The LET statements in the INPUT section assign the known data to variables.
    • Mr. Blue is alphanumeric data, so it is assigned to the string variable employee1$. String variables always end with a $.
    • 23 and 5.25 are numeric data, so they are assigned to the numeric variables hours1 and rate1, respectively.
  4. In the PROCESSING section, the LET statement consists of numeric variables -- not the actual numeric values.
  5. The calculation is performed using the numeric data assigned to the variables.
  6. The results of the calculation are assigned to the numeric variable wage1.
  7. The PRINT statement in the OUTPUT section, displays a sentence by combining literals (inside of quotation marks) and variables. Notice the literals are in quotes but the variables are not. The use of semicolons (;) separate the literals from the variables and tells the computer to keep PRINTing on the same line. Also, to get the proper spacing, blanks were included inside the quotation marks. Finally, the $ in front of 120.75 was forced there by being included inside the quotation marks.

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.

  1. Change the output statement. When the program is run, it should display:
    Mr. Blue will be paid $120.75 this week.
    Remember to use the variables. Don't simply print a literal. RUN your program to make sure the new output sentence appears correctly.
  2. Change the name of the employee from Mr. Blue to Mrs. Greene.
  3. Change her hours worked to 34.25.
  4. Change her rate of pay to 6.00.

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 Review

Before moving on to the next assignment, be sure that you can do the following:

  1. Explain the importance of algorithms to the development of computer programs.
  2. Explain why programmers use flowcharts to represent algorithms.
  3. Distinguish between numeric and alphanumeric data.
  4. Use an analogy to explain how a computer stores data.
  5. Define the following terms: variable, variable name, numeric variable, string variable and assignment statement.
  6. Use the key word LET to assign data to variables.
  7. Correctly use the rules for naming variables.
  8. Modify a simple program using numeric and alphanumeric data.

    This lesson is extremely important. You will be using variables to write programs throughout the rest of the course. Make sure you clearly understand the concepts that have been presented.

 

 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

previousnext