Are you sure you want to show the source code ?Have you tried to think it, with what we have learned ?

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.

prerequisite

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 turtleset i =0;

algo.setColor(10);// init color to green
loop (4){
i++;// counter, used for next examplesalgo.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:

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

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.

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 turtleset i =0;

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

if(i ==1|| i ==3){// one on twoalgo.setColor(13);// magenta}else{// so 2 and 4algo.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 turtleset i =0;

loop (4){if(i >=2&& i <=3){// between 2 and 3algo.setColor(13);// magenta}else{// so 1 and 4 possibilitiesalgo.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}}

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.