Task 5  Task 5: Introducing Variables  Task 5


-- Using variables in our program --

Wouldn’t it be nice if our program can actually say “Hello” with the user's name instead of saying the generic “Hello World?” In order to do that we must first ask the user for his/her name and then store it somewhere and then print out “Hello” with the user’s name. Let’s see how we can do that by typing the following into our IDE:

PRINT "Enter your name:"
INPUT user$
PRINT "Hello " + user$

 When you type and execute this program, you’ll see an output as shown in Figure 1 below:

Waiting for name
Figure 1 - Waiting for the user's name

And when you type in your name and hit ENTER, you’ll see the following output as shown in Figure 2 below:

A Warm Hello
Figure 2 - A warm hello :)

Now, if you run the program again, you’ll be asked the same question again. You can type in a different name and the computer will say Hello with that name.

-- Program Analysis --

You were introduced to the PRINT command in Task 4 so the first line of our code should look familiar. The second line however contains a new command that can be used to gather data from the program user, the INPUT command. The INPUT command, as used here, will accept input from the keyboard and store it for later use. The data gathered is then stored into a variable, or a place holder for information. The programmer must give the INPUT command the name of the variable to store the information into. In this program's case the variable name we decided to use is user$. The dollar sign at the end of the variable is known as a type declaration and denotes that the type of data this particular variable is to hold will be literal string information. When reading this variable aloud you would not say "user dollar sign" but instead "user string".

Because user$ is denoted as a literal string type the user of the program can type in any characters they wish for an answer and the variable will hold that information. The variable grabs a portion of the computer's RAM and claims it for itself. This location in RAM can then be called upon later as we see in the third line of the program. The PRINT command is being used once again to print a message to the user, "Hello ", but also the contents of user$. The use of the plus sign ( + ) in between "Hello " and the variable tells the PRINT command to add the two strings together to make one larger string to be printed. In computer science the act of adding two or more strings together is known as concatenation. Seriously, I'm not making this stuff up. So, before the PRINT command in the third line prints anything to the screen, it first adds Hello and whatever is stored in user$, Mister Moose in the case of Figure 2 above, creating the new string "Hello Mister Moose" and then this new string is printed on the screen.


-- Variable Types --

Think of a literal string variable as a snapshot of data. You'll learn commands later on that can manipulate data strings but for the most part the computer, and the program, really have no idea what kind of information is being stored in a string. It's up to the programmer to keep track of that information and use it as needed. QB64 offers many different variable type declarations but in this course we will focus on the following most commonly used types:

INTEGER ( % )

An integer type variable can hold any whole number value in the range of -32768 to +32767. Integers are processed very quickly by QB64 programs and should be used as much as possible for small integer values. If you assign a number with a decimal portion, such as 200.34, the decimal portion will be cut off, or truncated, leaving only the whole number portion of 200 being stored.

LONG INTEGER ( &)

A long integer type variable can hold any whole number value in the range of -2,147,483,648 to +2,147,483,647. Long integers are processed very quickly by QB64 programs and should be used as much as possible for larger integer values. If you assign a number with a decimal portion, such as 200.34, the decimal portion will be cut off, or truncated, leaving only the whole number portion of 200 being stored.

SINGLE PRECISION ( ! )

A single precision type variable can hold any real number value in the range of 2.802597E-45 to 3.402823E+38. Single precision numbers are handled by QB64 a bit slower than whole integer numbers because of their complexity in handling decimal points.

DOUBLE PRECISION ( # )

A double precision type variable can hold any real number value in the range of 4.490656458412465E-324 to 1.797693134862310E+308. Double precision numbers are handled by QB64 much slower than whole integer numbers because of their complexity in handling decimal points and their amazingly large size.

STRING ( $ )

A string type variable can hold any characters and can range in size from 0 characters (a null string) to 2,147,483,647 characters (a novel such as War and Peace if you like!)

-- Variable Naming Rules and Good Practice --

There are a few rules that need to be followed when creating variable names in QB64. Most of these rules are also universal amongst most programming languages available today.

- Variable names must start with a letter of the alphabet therefore numbers can't be used at the beginning. For example  OneTime$ is a valid variable name but 1Time$ in an invalid variable name.

- Variables names can only contain combinations of uppper and lower case letters, numbers and underscore ( _ ) characters. Examples of valid variables names are First1%, User_Name$ and My___Variable!

- Variable names can't be named with a QB64 command name, otherwise known as a reserved word. Examples of invalid variable names would be Print$ and Input% since PRINT and INPUT are QB64 command names, or reserved words.

- Although not a rule, it is good practice to name your variables in such a way that they give meaning to the information that they intend to hold. For example, a variable that holds the user's current score might be named PlayerScore& or a variable that holds the current tax rate might be named as TaxRate!


- Also not a rule, but again considered good practice, is to declare variable names and their types that will be used throughout the program. Declaring variables at the beginning of a program sets aside the RAM space needed right away for variable storage and makes a handy table for the programmer to refer back to. Most languages actually require that this be done, but BASIC does not. There will be more discussion on this later.

-- Code Examples --

The best way to learn how to use variables is to write some code! Let's start out by modifying the program you typed in at the beginning of this task. Once a variable has some information stored in it you can use that information over and over. Furthermore you can change a variable's contents at any time you wish. Make the following changes to your code and then execute the program to see it in action.

PRINT "Enter your name:"
INPUT user$
PRINT "Hello " + user$
PRINT "How is " + user$ + " feeling today?"
INPUT user$
PRINT "What a coincidence, I'm feeling kind of " + user$ + " myself."

Excellent
Figure 3- Reusing a variable's contents and the variable itself

As you can see in the example code we used the contents contained in user$ twice in lines three and four. However, in line five we are asking the user to type in some more information and this input will overwrite the data currently contained in user$ because we are using the variable user$ again. This is a very common thing to do in programming. For example, let's say the player of our game can earn move up the ranks with experience. We might create a variable called Rank$ and initially set the value to "Private". As the player progresses to the next rank we set Rank$ to the value of "Lance Corporal", and then to "Corporal", and then to "Seargent" and so on. Heck, instead of imagining this let's go ahead and create some code that does this. Type the following code in and then execute it to see it in action. Save the current example program as HELLO2.bas before beginning a new program.

Rank$ = "Private"
PRINT "Ok " + Rank$ + ", answer the questions correctly to rank up!"
PRINT "What is 2 + 2 ?"
INPUT Answer%
IF Answer% <> 4 THEN END
Rank$ = "Lance Corporal"
PRINT "Good " + Rank$ + "! What is 5 + 3 ?"
INPUT Answer%
IF Answer% <> 8 THEN END
Rank$ = "Corporal"
PRINT "Excellent " + Rank$ + "! What is 10 + 5 ?"
INPUT Answer%
IF Answer% <> 15 THEN END
Rank$ = "Seargent"
PRINT "Outstanding " + Rank$ + "! What is the square root of 324 ?"
INPUT Answer%
IF Answer% <> 18 THEN END
Rank$ = "General"
PRINT "Out-freakin' standing Marine! You are promoted to " + Rank$ + "!"

Rank up
Figure 5 - How do you rank?

So far we've learned how to get input from the keyboard and store it into a variable, but as you can see in line one of our new code you can set variables right inside the code using the equals sign ( = ). Since we created Rank$ as a variable type of string (because of the $) we can only insert string values into it. Therefore, any information we store into it must be a string as well. This is the reason line one contains the word Private in quotes, as the quotes denote string data. If we would have entered line one as follows:

Rank$ = Private

the IDE would have flagged this line as an error, because Private would be seen as another variable that holds numeric information. In essence you would be trying to assign a number to a variable that is supposed to hold a string. (It is possible to create variables without type identifiers, such as Private. If you do not specify a type identifier at the end of a variable it will default to a single precision variable that is only allowed to contain numeric information). By placing quotes around the word Private, you have identified this word as a string of information, therefore Rank$ will readily accept it and remember the value.

In our example code we only use two variables, Rank$ and Answer%, over and over again, changing their contents as the program progresses. Since all of the answers we expect from the user are whole numbers in the integer range we declared Answer% to hold this type of information by assigning the integer type declaration which is the percent sign ( % ). Don't worry about the IF statements in the code for now, they will be explained in a later task. Save this example program as RANKUP.bas before continuing on.


-- General Math 101 --

Oh, here we go, the part everyone has been waiting for, and possibly dreading when it comes to programming ... math. Well don't worry private, we'll start out easy and have you ranking up to general in no time. (And quite possibly have you creating programs that do your math homework for you! Shhhhh.... don't tell anyone!)

Simple math can be peformed within your code on variables with surprisingly relative ease. For now we'll focus on the basic mathematics operators:

- Plus ( + ) : used to add to numeric values together

- Minus ( - ) : used to subtract a numeric value from another numeric value

- Multiplication ( * ) : used to multiply two numeric values together (use the asterisk, Shift-8)

- Division ( / ) : used to divide one numeric value into another numeric value (use the forward slash)

-- Playing With Numbers --

Let's write a few simple example snippets of code to see these mathematics operators in action shall we? Type in the following program and then execute the code to see it in action.

Number1% = 10
Number2% = 20
PRINT "10 minus      20 = "; Number1% - Number2%
PRINT "10 plus       20 = "; Number1% + Number2%
PRINT "10 times      20 = "; Number1% * Number2%
PRINT "10 divided by 20 = "; Number1% / Number2%

In the above example we set two integer variables to equal 10 and 20 and then do some basic math on them in the PRINT statements. But, did you notice something different this time about the PRINT statements? Notice the use of the semicolons ( ; ) in the PRINT lines. When a semicolon is used in a PRINT statement it is telling the PRINT statement not to move the cursor down to the next line, but instead whatever follows is to be printed to the screen from the position of the semicolon. So why didn't we just use the plus sign like we did in previous examples:

PRINT "10 minus      20 = " + Number1% - Number2%

Because this will generate an IDE error, that's why (change your code and see for yourself). Let's take a look at what the line above is attemtping to do. By placing a plus sign in between the literal string and the variable Number1% you are instructing the PRINT command to concatenate a string with a numeric value. Concatenation can only happen between two strings and since this is not the case the IDE will generate the error. So, by using the semicolon we are actually telling the PRINT command that there are two parts you need to display to the screen, a string, and then a numeric value based off of one variable being subtracted from another. The semicolon ensures that the the results of the math equation will follow directly after the literal string on the same line.

Math can also be performed within the source code and saved directly to a variable as well. Type in the following code snippet and execute it to see it in action. Save your current example as MATH1.bas.

Number1% = 10
Number2% = 20
Answer1% = Number1% - Number2%
Answer2% = Number1% + Number2%
Answer3% = Number1% * Number2%
Answer4% = Number1% / Number2%
PRINT "10 minus      20 = "; Answer1%
PRINT "10 plus       20 = "; Answer2%
PRINT "10 times      20 = "; Answer3%
PRINT "10 divided by 20 = "; Answer4%

In this example we've created four new variables, Answer1% through Answer4%. We then perform our four math equations and store the results into each respective variable. We then use the PRINT command to show the results on the screen. By pre-computing our values and placing them into variables we can use the results later on if we need them. But, did you notice something terribly wrong with our program's output screen?

Wrong!
Figure 5 - HAL wasn't the brightest accumulator in his elemetary math class (I heard that Dave)

Can you guess why the variable Answer4% contains the wrong value? Is it a bug in the Pentium processor? Perhaps your math teachers have been lieing to you all along, how can a computer be wrong, right? The answer lies in the type of variable that Answer4% is. Since we declared the variable Answer4% as an integer type by assigning a percent sign, it can only hold whole numbers. 0.5 is not a whole number so Answer4% will only hold the whole number portion which is zero! As a programmer you will need to keep variable types in mind all the time to avoid making these kinds of mistakes. They can literally drive you nuts!

True Story: A game I made a while back called Super MegaBug had a bug in it (go figure, lol) where the player's score would mysteriously reset back to zero. This always seemed to happen when I wasn't paying attention to the score while Beta testing the game. For over a week I searched and searched for the cause of this, pouring over my source code with a fine toothed debugger (me). Then it finally hit me, I had assigned the player's score to an integer variable. When the player's score would reach higher than 32,768 (the upper limit of an integer) it would reset back to zero. Changing the player's score variable type to a long integer corrected the problem, but there was no joy for that week of looking for that silly bug that I had caused because I didn't pay attention to detail when writing my code.

Let's go ahead and fix our code so the correct value is computed. Change line six to read:


Answer4! = Number1% / Number2%

and then change line ten to read:

PRINT "10 divided by 20 = "; Answer4!

What we did in these two lines is the same thing I did to fix my Super MegaBug game. We changed the variable type to something that can accept the result needed, in this case to a single precision ( ! ) variable.

That's Better
Figure 6 - That's a good computer

Save this example code as MATH2.bas now before we move onto the next topic.

-- A Practical Program --

It's time to put everything we have learned so far into one program that has a practical use. Here in the United States we still use the Fahrenheit scale for temperature while most of the world uses the Celsius scale. Converting from Fahrenheit to Celsius is not a difficult task if you know the formula and have a calculator handy, but how many people do you know that can do the conversion in their head? This is a perfect example of where a simple conversion program can come in handy. Type in the following code and execute it to see it in action.

PRINT "Fahrenheit to Celsius Converter"
PRINT "-------------------------------"
PRINT
INPUT "Enter temperature in Fahrenheit : ", Fahrenheit!
Celsius! = 5 * (Fahrenheit! - 32) / 9
PRINT
PRINT Fahrenheit!; "degrees F ="; Celsius!; "degrees C"

One of the greatest features of BASIC is that very little code can do so much. With just seven lines of code we have written a practical program that solves a problem. The first three lines of code are simple PRINT statements setting up the look of the program. In the fourth line however we are using the INPUT command in a new way. The INPUT command can optionally display a string of text before waiting for keyboard input. So instead of using a PRINT statement one one line to ask the question and then the INPUT statement on another line to wait for keyboard input, as in previous examples, we can combine both functions into one statement. The use of a comma after the literal string question allows you to put the name of the variable that will hold the entered data, in this case Fahrenheit!. Line five contains the conversion formula for converting Fahrenheit to Celsius and stores the result in the variable named Celsius!. The formula on line five follows the order of operation you learned in elementary math class, that is, do the calculations contained in parethesis first, followed my multiplication and division and then addition and subtraction. For instance, if the user entered the value of 72, the order of operations would be as follows:

Paranthesis first: (72 - 32) for a result of 40

Multiplication next: 5 * 40 for a result of 200

And finally division: 200 / 9 for a result of 22.222 repeating

The final answer of 22.222 repeating would then be stored into the variable Celsius! for later use. Finally, in the last line of the code the variables and literal strings are brought together with a PRINT statement to give the user the answer desired. Save this example program as F_TO_C.bas before continuing on to the next section.


-- Variables "Out of the Blue" --

So far in all of the example programs we have done the variables were just created as needed. BASIC was designed to allow this kind of functionality, because remember that BASIC was created to be easy to learn for the beginner and simple to use for the person needing some programming skill in their profession. But, in the programming world this is considered bad coding form and most other languages require that they be told beforehand, or declare, the variables that will be used throughout the program. In this course you will be required to declare all of the variables your program will use. Not declaring your variables would only create a bad habit that would be hard to break when you move on to another language.

Let's modify our Fahrenheit to Celsius converter and this time declare the variables we are going to use. Make the following changes to your code.


'--------------------------------
'- Variable Declaration Section -
'--------------------------------

DIM Fahrenheit! ' holds the user's input temperature in Fahrenheit
DIM Celsius! '    contains the formula's computation in Celsius

'----------------------------
'- Main Program Begins Here -
'----------------------------

PRINT "Fahrenheit to Celsius Converter"
PRINT "-------------------------------"
PRINT
INPUT "Enter temperature in Fahrenheit : ", Fahrenheit!
Celsius! = 5 * (Fahrenheit! - 32) / 9
PRINT
PRINT Fahrenheit!; "degrees F ="; Celsius!; "degrees C"

One of the uses of the DIM statement is to declare variables that will be used throughout the program. The DIM statement sets aside the needed location in RAM to store the variable's information. The benefits to declaring your variables is:

- It forces the programmer to think about the code before actually coding. Good coding comes from a good thought process of the code design and code flow before the fingers hit the keyboard.

- It creates a very useful table of variables used throughout the program for programmer reference.

- It creates a BASIC programmer with good coding habits for other languages that require variable declaration.

Go ahead now and resave this modified program as F_TO_C.bas. When asked answer yes to overwite the old code.

-- Non-Variable "Variables" --

There is a special type of variable known as a constant that never changes its value once it is set. Constants are useful for numbers that never change their value, such as Pi. Type in the following example and execute it to see how constants can be useful.

'--------------------------------
'- Variable Declaration Section -
'--------------------------------

CONST PI! = 3.1415926 ' the value of Pi that never changes

DIM Radius! '           the radius of the circle supplied by the user

'----------------------------
'- Main Program Begins Here -
'----------------------------

PRINT "Circle Circumference Calculator (Pi times r"; CHR$(253); ")" ' CHR$(253) explained later
PRINT "---------------------------------------------"
PRINT
INPUT "Enter the circle's radius > ", Radius!
PRINT
PRINT "The circumference of this circle is"; PI! * (Radius! * Radius!)

The CONST keyword is used to declare a variable as a constant value. Once a constant has been set it can't be changed anywhere in the program and attempting to do so will cause the QB64 IDE to generate an error. Many programmers create constant variables in all UPPER CASE to make them easy to distinguish throughout the source code. Go ahead and save this example as R_TO_C.bas before continuing on.

-- No Comment --

While "no comment" may be a necessity for politicians or movie stars in sticky situations this credo is very bad for coders. In the code example above you see that there are comments added to the code through the use of the REM statement, which can be abbreviated through the use of an apostrophe ( ' ).  This code:

'--------------------------------
'- Variable Declaration Section -
'--------------------------------


and this code:

REM --------------------------------
REM - Variable Declaration Section -
REM --------------------------------


actually mean the same thing to the QB64 IDE, but the abbreviated version ( ' ) looks cleaner and all BASIC coders use the abbreviated form. The REM statement is used to "Remark", or comment, the source code. REM statements are a great way to separate different areas of the code for readability and to comment a single line of code as these two lines did:

CONST PI! = 3.1415926 ' the value of Pi that never changes

DIM Radius! '           the radius of the circle supplied by the user


Now, not only do you have a declared listing of variables used throughout the program, you also have a comment after each explaining their use as well! The QB64 compiler simply ignores anything after the REM statement, or ( ' ), on a line. Remarks are for the programmer and are an absolute necessity, especially when code gets to be hundreds, thousands or even millions of lines long (Windows may well be over 100 million lines of C code now). In this course you will be required to use comments in your code to explain your code's design and flow. All programmers develop their own style of programming and therefore it is essential that a programmer reading someone else's code has the original coder's remarks to refer to when just the code itself is confusing to follow.

-- Your Turn --

Create a program that converts inches to millimeters using the programming techniques you have learned to date. The program must:

- Display it's purpose to the user

- Ask the user for the amount of inches to convert.

- Output the result of the conversion to the user on the screen

- Variables must be declared at the beginning of the code

- Each declared variable must contain a comment after it as to its purpose

When finished save your program as I_TO_M.bas.



-- COMMAND REFERENCE --


Commands learned in previous tasks:

PRINT

New commands introduced in this task:

INPUT
DIM
REM or '
CONST

Concepts learned in previous tasks:

execute
statement
expression
literal string
syntax error

New concepts introduced in this task:

variables
type declarations
literal strings
concatenation
integers
long integers
single precision
double precision
strings
null strings
reserved words
operators ( +, -, *, / )
declare
constant