Examples of uses of variables in programs

Examples of uses of variables in programs
Keeping a count
In quizzes and games, score and lives are often used to manage a player’s success or failure. The value in the variable ‘Score’ is increased when a player is successful, say when they answer a quiz question correctly. Performing a calculation
In programs, we may need to perform some kind of calculation, in which case we may need to use variables to hold the values involved. This often happens in simulations and modelling programs. For example, if drawing shapes, pupils can calculate the external angle of any regular polygon using the number of sides of the shape. Variables can be used to store the calculated angle, use it as the shape is drawn and and display it to the user. A program that draws any regular polygon, the user enters the number of sides of the shape they want to be drawn and the program uses variables to calculate the angle needed to draw the shape and control the loop to draw the shape. 1 The output from a shape drawing program, shows the variables used in the program. Using random numbers
If creating a numeracy quiz, a random number generator can be used to pick values for questions. In order to use these numbers both to display the question and then to check the players answer the random numbers need to be stored in variables. Variables are used to store random numbers so that the question can be asked and the answer calculated and checked. Controlling the flow of a program using a variable
If creating a quiz, we might ask the user if they want an easy or hard quiz. What the user responds with can then be used to control which part of the program runs. A variable is used to control what the program does. Control of output devices
If controlling the speed of a motor that drives a fan to keep us cool, we might create variables that are set by the user to control how long the fan works for, and how quickly it spins. 2 The output from a control program, shows the variables used in the program. Variables are used to control the power and run time of the fan. Data logging
If creating a sound monitor that logs the noise level in a class, we could use a number of variables in the program including a user created list (array) called ‘sound levels’ to log the data, a user defined variable ‘time_ check’ to work out when we should log the next ‘sound level’ and predefined variables such as ‘loudness’. 3 The output from a data logging program, displays some of the variables used in the program. Variables are used to record the how noisy it is in class saving the data into a list and using variables to control how often the sound is checked. Storing constants
Variables can also be created to store a constant value so that we can refer to it by name. Using a variable to store a constant ‘Pi’. 4 Controlling loops
One particularly useful example of variables in programming is as an iterator (or counter) – this is a way of keeping track of how many times you’ve been round a repeating loop and of doing something different each time you do. To do this, we initialise a counter to zero or one at the beginning of the loop and then add one to it each time we go round the loop. This script would get Scratch to say its eight times table. 5