Computer Studies‎ > ‎Turing‎ > ‎Turing Tutorial‎ > ‎

Turing 015 — Integer Variables

Variables are temporary storage areas in memory that can be used to store text (called “strings” in programming lingo) or numbers. For instance, a variable called “numPets” could be used to store the value 3 at one time or 5 at another.

To use a variable in Turing, you must “declare” it. For example, to use an integer variable called “numCars” that is used to represent an integer number you need the following declaration statement:

var numCars : int

To assign a value to this variable you use the assignment statement

numCars := 3

Remember that this is an integer variable, meaning that it can only be used to store numbers such as 1, 52, or 1034. It cannot be used to store "real" numbers such as 1.2, 9.7, 0.03. These will be covered later.

Also remember that you should use variable names that are meaningful. For instance, so not use "number" as a variable name when something like "length" or "numOfApples" would be more suitable.

Assignment

Create a program that assigns the value 7 to a variable called "luckyNumber", then prints “The value of luckyNumber is <value of luckyNumber>”.

Save as "015.t".

In Turing, variable names may be as many as 50 characters long. The first character must be an alphabetic character or an underscore ("_"). Variables may be either uppercase or lowercase, and the computer distinguishes between the two. This means that the name "TA4" is not the same as "ta4". The rest of the characters may be any combination of the digits 0-9, alphabetic characters or the underscore. No spaces or symbols may be included in variable names.

Variable names that have meaning in your program are encouraged. Using the variable "age" rather than the variable "x" to represent a person's age would be more understandable, as would using the variable "grossPay" rather than "a". At the time you are entering a program, you may feel this to be quite time consuming, however, if you have to make any modifications later, you will find that your time was well spent.

With Turing, the variable names cannot be Turing keywords such as "var" or "put". You do not, however, have to worry about variables containing keywords. For example, the variable "_output" could be used even though it contains the keyword "put".



Comments