1.4 Using Variables

So far we've talked about how to declare variables. Now it is time to start using them. In this section we'll look at how to assign values to variables and how to print the value stored in a variable. Here is a program that does both of those things with several variables of different types.

var team : string
var win, loss : int
var percentage : real

team := "Toronto Maple Leafs"
win := 38
loss := 25
percentage := 0.603
put "The ", team, " have ", win, " wins."
put "They have ", loss, " losses."
put "This gives them a winning percentage of ", percentage 

This program produces this output:

The Toronto Maple Leafs have 38 wins.
They have 25 losses.
This gives them a winning percentage of 0.603

To assign a value to a variable you put a colon equals after the name of the variable followed by the value you wish to store. For now we are just storing contants. Notice that text in quotes gets printed exactly as it is. When you put the name of a variable by itself the value contained in the variable is printed.

You can set the initial value of variables at the same time that you declare them. You do this by putting a colon equals followed by the value after the variable declaration on the same line. The previous program could be shortened like this:

var team : string := "Toronto Maple Leafs"
var win : int := 38
var loss : int := 25
var percentage : real := 0.603

put "The ", team, " have ", win, " wins."
put "They have ", loss, " losses."
put "This gives them a winning percentage of ", percentage 

In the above example if we had left win and loss on the same line and tried to initialize like this:

var win, loss : int := 38 

then both win and loss would get initialized to 38.

You need to make sure that you assign the proper type of values to each variable. The following program has several errors. Each line with an error has a comment explaining the error.

var number, x : int
var decimal : real
var word : string

number := 1.234        % can't store decimals in int variables
decimal := 5
number := 123E6        % E notation numbers are always reals
word := missing quotes % strings need double quotes around them
put x                  % x doesn't have a value yet
nonexistant := 11      % nonexistant hasn't been declared

Notice that you are allowed to store ints in real variables. Turing sets aside different amounts of space in the memory of the computer depending on what type of variable you are using. That is why you need to declare variable types. The int type takes up less space than the real type. That is why it is O.K. to store an int in a real variable, but not the other way around.

You may be wondering why we've been using variables instead of constants. So far we haven't changed any of our so-called variables. Here is a program to prove that variables can vary:

var number : int := 10

put number
number := 5432
put "Now number is ", number
number := -99
put "And now it is ", number 

Looking at the output you can see how number has three different values. When you assign a value to a variable, any previous value is destroyed and is replaced by the new value. This is the output:

10
Now number is 5432
And now it is -99


Exercise 1.4

  1. The following program has five errors. Find them and correct them.
    var age : int
    var height : real
    
    age := 15.0
    put "Age in years is ", Age
    put "Height in metres is ", height
    height := 1.65
    mass := 71
    put "Mass in kilograms is ," mass 
  2. Write a program that uses variables engineSize, numberOfCylinders and carMake to print the engine size (in litres), the number of cylinders and the type of car. Here is a possible sample output:
    Honda Accord
    Engine Size in Litres: 1.995
    Number of Cylinders: 4