prerequisite

basics

We have seen what is a loop
It is possible but not intuitive to know number of each turn.

loop turn:

1

2

3

4

5

6

7

set i =0;// initialize
loop (4){// condition if counter is < 4 then continuealgo.text("actual turn is "..i);
i++;// equivalent to i = i + 1}

Notice the previous code : the loop run 4 times.
So i is initialized to 0 and is incremented 4 times too : 0, 1, 2, 3
Not 4, because 0 is consitered as an iteration.
This behaviour is not intuitive, but it makes sense if incrementation is done at the end of the loop like here.
It will be the same behaviour with the "for" instruction.

"For" instruction is designed to do this on a single line.
It allow to declare a variable, initialize it and increment it each time it loop.
The code below can be translated as following:

for

for

For is a complexe but usefull instruction.
It allows to repeate a number of time an instruction (or, as seen above, a block).
For is constituted as follow :
[for keyword] ([variable initialization]; [exit condition]; [iteration]) [instruction(s)]
The sequence is :

1 At the first time for is read, variable is initialized.

2 Condition is verified, if ok, execute instruction, else exit.

3 Iteration is executed.

4 Goto 2 (while condition is verified).

even a square 1:

1

2

3

4

5

6

7

// even a squarefor(set i =0; i <4; i = i +1){algo.go(100);algo.turnRight(90);}// end of loopalgo.hide();

Translation:

Initialize, create i variable and set it to 0

Verify if i is less than 4, yes it is equal to 0 ? Ok continue
Move to 100
Turn to the right from 90Â°
Iterate, i = i + 1 so i = 0 + 1 => i = 1

Verify if i is less than 4, yes it is equal to 1 ? Ok continue
Move to 100
Turn to the right from 90Â°
Iterate, i = i + 1 so i = 1 + 1 => i = 2

Verify if i is less than 4, yes it is equal to 2 ? Ok continue
Move to 100
Turn to the right from 90Â°
Iterate, i = i + 1 so i = 2 + 1 => i = 3

Verify if i is less than 4, yes it is equal to 3 ? Ok continue
Move to 100
Turn to the right from 90Â°
Iterate, i = i + 1 so i = 3 + 1 => i = 4

Verify if i is less than 4, no it is equal to 4 ? Exit and continue after the block
Hide the algo's turtle

step

With for loop, it is possible to count with another step than 1.
For example to count from 2 to 2.
To do this, two things are necessary:

Change the step counter in for instruction

Be carefull to have not exceed the condition without verify it. Prefere use < or > instead of equal

from 2 to 2:

1

2

3

4

// count from 2 to 2for(set i =0; i <=8; i = i +2){text.output("From 0 to 8 step 2 : "..i);}

The condition i < guaranty if i is superior, the loop still exits.
The i = i + 2 will increment i by 2 each loop.

countdown

It is also possible to use for loop tou count down.
To do this, two things are necessary:

Change the step counter in for instruction by a negative number

Invert the initialisation and the condition to go from maximum number to minimum one

Program will go from 8 to 0 by countdown
It is initialized with 8 and condition verity that i is greater or equals to 0
Execute it in step by step mode to see what it does.
Note : Iteration i = i - 1 can be written i--

User defined polygone

// user askingset nbEdge =text.inputNumber("How many edges ?");set size =text.inputNumber("What is the size of each edge ?");for(set i=0; i<nbEdge; i++){// loop on number of edgesalgo.go(size);algo.turnRight(360/ nbEdge);// a total rotation divided by the number of edges}