Variables 2016-12-22T10:17:55+00:00

Chapter 2: Using Variables

Box5YComputer programs normally use (or process) data in some way to produce results.  This normally relies on an INPUT from the program or user, which is then PROCESSED by the program and finally displayed in some way in the console window, i.e. OUTPUT.

Variables - Input - Process - OutputThis shows the structure of a Small Basic program.

The input to the program is often supplied by the user whilst the program is running.  In order to be able to do this the program must have some way of remembering the values that have been input, and you, the programmer, needs to know how to access them to make the program process what they contain.  To do this we use a feature called VARIABLES.

The assignment statement

To store something in a variable you need to use an assignment statement.  For example:

Variables - assignment

The example on the left stores the value 8 to the variable called NumberOne.

The ‘=’ is the assignment operator; the value on the right is what is stored in the variable that is identified on the left of the assignment operator.  It can be read as “becomes equal to” or “takes the value”.  Don’t think of it as ‘equals’ as this is not true until after the assignment has been made.

Variables - Assigning resultsAn assignment statement can also contain an expression (calculation) on the right side of the ‘=’, which will be evaluated when that statement is executed.

The above statements will result in the variable SUM ‘becoming equal to’ the value 23.  Notice that I have added white space to the above assignments just to make it easier for me to read.  Remember, Small Basic ignores the white space so, if it makes it easier for you, use it.

The TextWindow.WriteLine() Statement

Variables - outputting value storedIf you want to display the contents of a variable, you need to use the TextWindow.WriteLine statement and provide the variable identifier in brackets:

Variables are NOT put inside quotation marks as this would make them into what is called a literal.  Using TextWindow.WriteLine( “Sum” ) will output the word ‘Sum’ not what the variable Sum contains.

Variables - outputting multiple contentTo output a sentence made up of variables and literals on one line of the Text Window you should use the output statement TextWindow.Write() to display each individual item as shown below.  This displays its output without going on to the next line.  The example on the left is shown in the image on the right below:

Variables - on screen evidenceNotice that only the spaces inside the quotes are output to the screen.


Variables Box5PIn a computer program, variables can be imagined as labelled boxes that store data for use in the program.  In Small Basic you tell the computer that you want to store data in a variable when you use it for the first time.

Using the example of a program that allows the user to enter two numbers so that the computer can add them together and display the sum: three variables are required as shown below:

Variables - a representationYou can imagine the variables as boxes like this, but in reality they are memory locations in the computer’s main memory storing byte patterns to represent the value you want stored.

In the above diagram, the ‘Box labels’ are known as identifiers.  As the programmer it is for YOU to choose an appropriate identifier for each variable you use in your program.  A valid identifier must start with a letter and can consist of any combination of letters, numbers and the underscore character (_), but not spaces.  It can have a maximum length of 40 characters which in practice this rarely causes a problem.

For ease of understanding by other programmers, (your teacher!) and yourself later on, you should choose an identifier that explains the purpose of your variable.  This is good practice and very useful when your programs get more complicated.  E.g. NumberOfEntries rather than n5 or number.  Using capital letters at the start of each word in the variable identifier is called ‘CamelCase’ and is also a good habit to get into.

NB: Small Basic is not case-sensitive, so that Number, number, nUMBER, NUMBER are all the same variable.

Complex Code
A complex statement is when you try to do too much in a single statement.  Sometimes they don’t work because the rules of a programming language are very strict and the computer gets confused.  More often they don’t work because you, the coder, got confused and missed out or added extra characters that were not needed.  Read this pop-up for an example.

User Input

To enable your program users to enter values while the program is running, you need to use two new TextWindow methods.  These methods are called Read() and ReadNumber().  They enable you to make your program wait until the user has typed a value and pressed the enter key. The panel below shows how you use them.

Variables - Inputing values
The following panel shows a small program that asks the user to enter two numbers to have them added up and the result displayed.  Notice that I have remembered to use comments to make sure my programming is clear. Variables - Add2Numbers Clip

Enter this program, saving it in a suitable location and run it. You should have the output shown below in your text window.Variables - Add2Numbers Output

This, however, is not very user friendly. If someone other than you ran this program without seeing the code that you have written, they would not know what to do. They would probably just press the enter key a couple of times and then the output really wouldn’t make sense. To be a good program it really needs to be much better so that the user knows exactly what to do and what the program has done when it ends.

Have a look at the panel the right (or below if you have a narrow screen) to see how it could (and should) be done.

Variables Box5P

User Input
 Making sure you get the right data input into your
program by the ‘user’ is important – take a look at this pop-up for more information.

End of Topic Tasks ?

User Prompts

Telling the program user what to do is just as important as giving them the results from your program.  After all, if they don’t know what to input to your program, how will it be able to give the correct information back.


The image above shows the same program as on the left, but this one has been improved to make sure the user enters reasonable data.

Add2Numbers2OutWhen this program is saved and run, the output to the TextWindow is far better and the end-user knows exactly what to do.  The visual quality can still be improved but that is covered in the next few chapters.