1.5 - If then else - Conditions

Sometime the program needs to take a decision.
It will determinate the behaviour, the intelligence of the program.
For example if the variable has this value, then do this action, else do this one.
Or if the user answer this value, then.... ect.
Conditional programing will give you the possibility to altering the program flow by verifiying some condition you have choosen.

synopsis

  1. prerequisite
  2. basics
  3. if then
  4. else, else if
  5. not
  6. other conditions
  7. operators
  8. it is your turn to play
  9. summary

  10. << 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 - Loops1.4 - Variables1.6 - For loop

basics

What are boolean type and condition ?

boolean

In previous tutorials, we have seen two data types: the number that represent all the numeric values manipulated in the program and the string to manipulate the sentenses.
We will study a third type, the boolean.
A boolean is a type that represents of only two states: true and false.

condition

A condition is a statement that must be verified:
For example, we want to modify program behaviour if a number is greater than another one. Or a sentense is different than another.

We will articulate this tutorial around the following example:

 
basic
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
algo.hide (); // hide turtle
set i = 0;

algo.setColor (10); // init color to green
loop (4) {
  i++; // counter, used for next examples
  algo.disc (50);
  algo.jump (100);
  algo.turnRight (90);
}

if then

If statement will execute the block of code if condition is verified at the point of program.
It's syntax is: if (condition) {block}
For example, we need to change the algo's color at the middle of the loop:

 
half spots magenta
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
algo.hide (); // hide turtle
set i = 0;
algo.setColor (10); // init color to green

loop (4) {
  i++; // counter
 
  if (i == 3) {
    algo.setColor (13); // magenta
  }
  algo.disc (50);
  algo.jump (100);
  algo.turnRight (90);
}

Notice that on boolean expression the symbol '==' is used to verify equality.
This symbol was choosen to distinguich it with assignation '='
So, the program will change color (contained into block) when i is equals to 3. As there algo keep the color, the next spots will keep the magenta color.

else, else if

else

Else statement will execute the block of code if condition is NOT verified at the point of program.
It is used after the if statement and its syntax is: if (condition) {block} else {block}
We will modify the previous code to change the spot when i == 2

 
spot 2 is magenta
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
algo.hide (); // hide turtle
set i = 0;

loop (4) {
  i++; // counter
  if (i == 2) {
    algo.setColor (13); // magenta
  } else {
    algo.setColor (10); // green
  }
  algo.disc (50);
  algo.jump (100);
  algo.turnRight (90);
}

else if

To make a suite of tests, it is possible to chain the if conditions
To test if a first condition is true, else a second, else a third ect....
Some "else if" instructions can be chained between first "if" statement and the "else" last one.
The syntax is : if (condition1) {} else if (condition2) {} else if (condition3) {} .... else {}
We will mofify our example to have a color for each point.

 
everythink has its color
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
algo.hide (); // hide turtle
set i = 0;

loop (4) {
  i++; // counter

  if (i == 1) {
    algo.setColor (10); // green
  } else if (i == 2) {
    algo.setColor (11); // cyan
  } else if (i == 3) {
    algo.setColor (12); // red
  } else { // 4
    algo.setColor (13); // magenta
  }
 
  algo.disc (50);
  algo.jump (100);
  algo.turnRight (90);
}

not

"not" is a boolean unary operator like minus in mathematic (example -7).
It represente the inverse of the boolean : not true is false and not false is true.
It's syntax is "!" in AL. So !true is false and !false is true.
To verify the non equality of too elements we will use the "!=" (not equals) opérator.
Example, verify of i is différent to 2 will be : i != 2 : i is not equals to 2.
It is possible, with boolean arithmetic, to define this differently by !(i == 2) : not (i equals to 2)
This second way will be used for more complicated operations.
So we will illustrate this by inverting the previous example:

 
spots 1, 3 and 4 are magenta
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
algo.hide (); // hide turtle
set i = 0;

loop (4) {
  i++; // counter

  if (i != 2) { // equivalent to if ( !(i == 2) )
    algo.setColor (13); // magenta
  } else {
    algo.setColor (10); // green
  }
  algo.disc (50);
  algo.jump (100);
  algo.turnRight (90);
}

other conditions

Equals and not equals are not the only comparison operators in AL.
This is the list of comparison operators :

  definition example
== equals to i == 2 : i is equals to 2
!= different to i != 2 : i is different to 2
< less that i < 2 : i is less than 2
<= less or equals to i <= 2 : i is less or equals to 2
> greater that i > 2 : i is greater than 2
>= greater or equals to i >= 2 : i is greater or equals to 2

operators

or

In boolean arithmetic, it is possible to make complexe operations.
"Or" is a binary operator like "+" in mathematic (2 + 2).
It is used to resolve operation like : i is equals to 1 or i is equals to 3.
It's syntax is "||" in AL. So to verify the previous condition we will write : i == 1 || i == 3
The is the complete table of or possibilities:

|| true false
true true true
false true false


We will modify the previous example to color the spots 1 and 3.
So we will test if i is equals to 1 or to 3:

 
spots 1 and 3 are magenta
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
algo.hide (); // hide turtle
set i = 0;

loop (4) {
  i++; // counter

  if (i == 1 || i == 3) { // one on two
    algo.setColor (13); // magenta
  } else { // so 2 and 4
    algo.setColor (10); // green
  }
  algo.disc (50);
  algo.jump (100);
  algo.turnRight (90);
}

and

"And" is a binary operator as seen with or.
It is used to resolve operation like : i is greater or equals to 2 and i is less or equals to 3. In other words, if i is between 2 and 3
It's syntax is "&&" in AL. So to verify the previous condition we will write : i >= 2 && i <= 3
This is the complete table of and possibilities:

&& true false
true true false
false false false


We will modify the previous example to color the spots 2 and 3.
So we will test if i is between 2 and 3:

 
spots 2 and 3 are magenta
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
algo.hide (); // hide turtle
set i = 0;

loop (4) {
  if (i >= 2 && i <= 3) { // between 2 and 3
    algo.setColor (13); // magenta
  } else { // so 1 and 4 possibilities
    algo.setColor (10); // green
  }
  algo.disc (50);
  algo.jump (100);
  algo.turnRight (90);
}

Notice that it is the same think of using "and" operator and using nested if.

 
nester ifs
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
set i = 5;
set j = 10;
if (i == 5 && j == 10) {
  // work
}
// Is the same thing than:
if (i == 5) {
  if (j == 10) {
    // work too
  }
}
 

it is your turn to play

Bi-color spiral

Create a be-color spiral as following by testing if iteration is 22.

 
Solution: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
algo.setColor (10); // initialize color
set i = 0;

loop (45) {
  i++; // counter

  if (i == 22) { // execute only if i is equals to 22
    algo.setColor (13); // change to magenta
  }
 
  algo.go (i * 10);
  algo.turnLeft (121);
}

Multi-color haze

Create a multi color haze as following image.

 
Solution: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
algo.setColor (10); // initialize color
set i = 0;

loop (40) {
  i++; // counter

  if (i < 10) {
    algo.setColor (10); // green
  } else if (i < 20) {
    algo.setColor (11); // cyan
  } else if (i < 30) {
    algo.setColor (12); // red
  } else { // the ten last
    algo.setColor (13); // magenta
  }
 
  algo.go (i * 10);
  algo.turnLeft (90);
}

Asking question

- Ask question to user : "do you like butter ? (yes / no)"
- If answer is "yes" tell : "ok, you like butter, hmmm it is good !"
- else tell : "oh! you do not like butter."

 
Solution: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
text.clear();
// answer
set answer = text.inputText ("do you like butter ? (yes / no) ");

if (answer == "yes") {
  text.output ("ok, you like butter, hmmm it is good !");
} else {
  text.output ("oh! you do not like butter.");
}

The timid

Write you first game!
The goal: guess a number between 0 and 9 choosen by the computer.
The rules are simples:
- to guess you have 3 tries
- if you are wrong the computer tell you if the result is greater or less than.
- if you are right, you win.

 
Solution: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
text.clear();
set tries = 3;
set found = false;
set number = math.random (9);

// main loop
loop (3) {
  if (!found) {
    set answer = text.inputNumber ("find number from 0 to 9:");
   
    if (number < answer) {
      text.output ("no! it is less than.");
    } else if (number > answer) {
      text.output ("no! it is greater than.");
    } else {
      text.output ("yes, you win!");
      found = true;
    }
  }
}

if (!found) {
  text.output ("poor player, you loose!");
}


I have a particular affection for this exercise, because it is this one that made ​​me understood programming when I was 9 or 10 years old.



summary

Summary

In this tutorial, you have learned:

  • what is a boolean ?
  • what is a condition ?
  • what to do else (if condition is false) ?
  • what are operators ?
  • how to use them to altering the program behaviours ?

Functions used

FunctionDescription