1.4 - Variables

We will see how to make dynamic programs using variables.
Variables provide a useful way to modify values without changing the code.

synopsis

  1. prerequisite
  2. basics
  3. declaration
  4. factoring
  5. modify
  6. concatenation
  7. more fun with random
  8. asking user
  9. it is your turn to play
  10. summary

  11. << back to tu(r)torials

prerequisite

Before reading this tutorial, it is important to have a good knowledge of: 1.1 - What is a program ?1.3 - Loops

basics

What is a variable ?
Put simply, variables are values that are allowed to change while the program is running.
More technically, it is an associated symbolic name (identifier) that contains information (a value).
A variable can be modified during the program execution, either by the program itself or by a user, e.g. in answer to a question.
An Al variables can contains several kinds of values :

  • Boolean: true or false
  • Number: e.g. 1, 2, 3, 45.7, -5
  • String: "my name is yann", "are you sure ?"

That's all for the moment. We will see the others types of variables in future.
Note that a string variable is always between two " symbols.

declaration

In Al, all variables must be explicity declared, i.e. you must tell Al what variables you want to use before you use them.
To declare a variable you need to use the set keyword.
Declaring a variable tells the program when the variable can be used from.
After that point the rest of the program will be able to use the value, but not before.
To assign a value to the variable, use the "=" symbol.

 
my name variable: 
  • 1
  • 2
  • 3
  • 4
  • 5
// my name is not known by the program before this line
set my_name;
my_name = "yann"; // my name is usable from here and after
algo.hide ();
algo.text (my_name);

Assigning the value of a variable can be done immediately upon declaration:

 
direct assigning: 
  • 1
set my_name = "yann";

factoring

Variables can be used to change program parameters. To illustrate this use our square example and modify it as follows:

 
parametric square: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
// size of the square
set size = 100;
algo.go (size); // variable used here
algo.turnRight (90);
algo.go (size); // variable used here
algo.turnRight (90);
algo.go (size); // variable used here
algo.turnRight (90);
algo.go (size); // variable used here
algo.turnRight (90);
 

At this moment, nothing has changed!
Now try changing the value of 'size' to 200, for example, and observe what is happens.
The size of the entire square has changed.

 
parametric square: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
// size of the square
set size = 200;
// following code does not change !
algo.go (size); // variable used here
algo.turnRight (90);
algo.go (size); // variable used here
algo.turnRight (90);
algo.go (size); // variable used here
algo.turnRight (90);
algo.go (size); // variable used here
algo.turnRight (90);
 

Do you see the advantage of using variables ?
You only need to modify the value of 'size' once to change the size of the whole square
Without this modification you would have to change it in 4 places.
We will see that there are more advantages of variables, too.

modify

Another advantage of using variables comes from their capacity to be modified. It is called mutability of variables.
e.g. a variable can be 5 at one time and later become 7 after some modifications in the program.
For example:

 
mutability: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
set a = 5;
// a is equal to 5
algo.text (a);

a = 7;
// a is now equal to 7
algo.text (a);


Another way to do this is to add 2 at the first value like this:

 
addition: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
set a = 5;
// a is equal to 5
algo.text (a);

a = a + 2; // or a += 2;
// a is now equal to 7
algo.text (a);


The operator += indicates that the initial value will be modified by adding the following number to it.
a += 2 is the same as a = a + 2;
All operators are described here: expression

concatenation

Numbers can be handled with arithmetic operators like +, -, * (multiply), / (divide) etc....
Strings have their own operator '..' to combine two strings together.

 
variable concatenation: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
// string 1
set str1 = "hi";
// string 2
set str2 = " android";
// result
set res = str1 .. str2; // res = "hi android"
algo.hide ();
algo.text (res);


It produces str1 followed by str2.
Note: as numeric operators, it is possible to work with variables and directly with values too.
The previous example could be rewritten as follows:

 
values concatenation: 
  • 1
  • 2
  • 3
set res = "hi" .. " " .. "android"; // res = "hi android"
algo.hide ();
algo.text (res);

more fun with random

Variables gives great flexibility to manipulate values.
An interesting function is the random one: math.random()
This function returns a number between 0 and the parameter we entered:

 
random: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
// get the random number
set a;
loop (4) {
  a = math.random (400);
  algo.circle (a);
}

The program will usually give a different circle diameter each time you run it.

asking user

It is also possible to ask the user of the program to supply the value it requires.
This is the foundation of user interface programming.
At this time, AL give two functions to interrogate the user: text.inputText (asking msg) and text.inputNumber (asking msg).
The function text.output (msg) is used to send simple message to user.

 
user choice: 
  • 1
  • 2
  • 3
// get the user number
set a = text.inputNumber ("what number would you like ?");
text.output ("you have choosen: " .. a);

it is your turn to play

Square parameter

Ask the user the size of the square to draw and draw it using the entered value

 
Solution: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
// asking user
set size = text.inputNumber ("what size would you like ?");
text.output ("you have choosen: " .. size);

// draw square using the size parameter
loop (4) {
  algo.go (size);
  algo.turnRight (90);
}

Random shape

Draw shape with 4 edges and 4 different random angles

 
Solution: 
  • 1
  • 2
  • 3
  • 4
  • 5
// draw shape with random angles
loop (4) {
  algo.go (100);
  algo.turnRight (math.random(360));
}

Run it a few times to see the difference each time

Random shape variation

Draw shape with 4 edges. At each corner turn the same angle, but a random one.

 
Solution: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
// determine the angle
set a = math.random (360);
// draw cube with angle
loop (4) {
  algo.go (100);
  algo.turnRight (a);
}

Run it a few times to see the difference each time

Ask user his name and say hello [name].

 
Solution: 
  • 1
  • 2
  • 3
// ast user
set name = text.inputText("What is your name ?");
text.output ("Hello " .. name);

Maze

Play with variable by incrementing it (size = size + 10) to obtain the following figure :

 
Solution: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
// maze
set size = 0; // initialize variable
// spiral loop
loop (45) {
  size = size + 10;
  algo.go (size);
  algo.turnLeft (90);
}

Triangular Spiral

Change the previous example to use a 121° angle. What happens ?

 
Solution: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
// spiral
set size = 0; // initialize variable
// spiral loop
loop (45) {
  size = size + 10;
  algo.go (size);
  algo.turnLeft (121);
}

You can play with a random value for the angle: set angle = math.random(360);



summary

Summary

In this tutorial, you have learned:

  • what are variables ?
  • how to use them to create dynamic and parametric program ?
  • how to add fun with random values ?
  • how to answer a question to the user of the programm ?

Functions used

FunctionDescription