ď»ż

# 1.6 - For loop

We have seen how make some loop.
It should be interesting to know the turn number in the block, shouldn't it ?
For instruction it here to help to do it.

## prerequisite

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

## 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 continue   algo.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:

inline for:
 1 2 3 4 5 for (set i = 0 /* initialize */; i < 4 /* condition */; i = i + 1 /* incrementation */)   algo.text ("actual turn is " .. i); }

Notice that i = i + 1; can be written i++;

## 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 square for (set i = 0; i < 4; i = i + 1) {   algo.go (100);   algo.turnRight (90); } // end of loop algo.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 2 for (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

countdown:
 1 2 3 4 // countdown for (set i = 8; i >= 0; i = i - 1) {   text.output ("From 8 to 0 step -1 : " .. i); }

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--

## it is your turn to play

### Create a pentagone

Draw pentagone : it is composed of 5 edges Solution:
 1 2 3 4 5 6 7 // loop 5 times for (set i=0; i<5; i++) {   algo.go (100);   algo.turnRight (360 / 5);   // a total rotation divided by the number of edges }

### Create a polygone

Draw polygone : it is composed of any number of edges Solution:
 1 2 3 4 5 6 7 8 9 // loop x times set nbEdge = 8; for (set i=0; i

### User defined polygone

Draw polygone witch number of edges and size is determined by user Solution:
 1 2 3 4 5 6 7 8 9 10 // user asking set nbEdge = text.inputNumber("How many edges ?"); set size = text.inputNumber("What is the size of each edge ?"); for (set i=0; i

### Nested loops

Draw the following figure Solution:
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // constants set nbGeom = 10; set nbEdge = 20; set size = 25; // first loop for geometries for (set g=0; g

Play with parameters to obtains some funny geometries

### Line Star variation

Draw ligne based star by incrementing edge size Solution:
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // constants set nbBranche = 20; set factor = 10; algo.hide(); for (set i = 0; i < nbBranche; i++) {   // draw branch   algo.go (factor * i); // use the loop variable i   algo.turnLeft (180);   algo.go (factor * i);   // return to original orientation   algo.turnLeft (180);     // turn for other   algo.turnLeft (360 / nbBranche); }

### Maze

With line star variation we have seen the importance of size changing on each iterations.
Try to draw the 4 edge spirale as followings: Solution:
 1 2 3 4 5 6 7 8 // maze set size = 0; // initialize variable // spiral loop for (set i=0; i<45; i++) {   size = size + 10;   algo.go (size);   algo.turnLeft (90); }

### Triangle Spiral

What are the modifications to do into code to obtain this following figure: Solution:
 1 2 3 4 5 // spiral for (set i=0; i<450; i=i+10) {   algo.go (i);   algo.turnLeft (121); }

### Spiral

Play with values to obtain this figure: Solution:
 1 2 3 4 5 // spiral for (set i=0; i<45; i=i+0.1) {   algo.go (i);   algo.turnLeft (10); }

### Star

Draw star that number of branches is defined by user Solution:
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // user asking set nbBranche = text.inputNumber("How many branches ?"); algo.hide (); for (set i=0; i
ď»ż

## summary

### Summary

In this tutorial, you have learned:

• what is a for loop ?
• how to use step ?
• how to use it for countdown ?
• what are the loop instructions and structure ?
• how to use them to create much and much beautifull drawings ?

### Functions used

FunctionDescription