1.3 - Boucles loops

Be careful! The 'loop' imperative instruction is only available from Algoid v0.4.0 beta.

We have seen how to write a sequence of instructions to make a program.
But have you noticed how difficult and boring it is to write the same sequence of instructions many times over?
Loop will help us to use 'factoring', to reduce the amount of code by repeating it for us.
It is the first instruction we will use to play with the program execution order.

synopsis

  1. prerequisite
  2. basics
  3. block
  4. loop
  5. it is your turn to play
  6. summary

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

basics

What is a loop?
A loop will repeat the same piece of code a number of times.
This piece of code is contained in a block.

block

A block will group a portion of code together, like it's only one instruction.
This code is contained in curly brackets {}.
In AL each time you see {} that means it's a new group of instructions.

 
block: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
// block
{
  // into block instruction 1
  // into block instruction 2
}
 

loop

Loop is a simple and useful instruction.
A limit is required to determine how many time the block will be repeated.

 
even a square 1: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
// even a square
loop (4) {
  algo.go (100);
  algo.turnRight (90);
}
// end of loop
algo.hide ();

it is your turn to play

Create a triangle

Draw a triangle with any loop
Tip: An equilateral triangle has all angles measuring 120° <=> (360 / 3)
(Note these are the angles that the turtle must turn. The angles inside a triangle always add up to 180°.)

 
Solution: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
// loop 3 times
loop (3) {
  algo.go (200);
  algo.turnRight (360 / 3);
  // a total rotation divided by the number of edges
}
 

Create a pentagon

Draw pentagon : it is composed of 5 edges and 5 corners

 
Solution: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
// loop 5 times
loop (5) {
  algo.go (100);
  algo.turnRight (360 / 5);
  // a total rotation divided by the number of edges
}
 

Line Star

Draw line-based star
Sequence of an line is:

 
an edge: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
// draw edge
algo.go (100);
algo.turnLeft (180); // u turn
// return to original position
algo.jump (100);
// return to original orientation
algo.turnLeft (180); // u turn
 
 
Solution: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
// loop for each branches
loop (20) {
  // draw branch
  algo.go (100);
  algo.turnLeft (180); // u turn
  algo.jump (100);
  // return to original orientation
  algo.turnLeft (180); // u turn
 
  // turn for other
  algo.turnLeft (360 / 20);

}

Star

Draw star
The sequence of one branch of the star is:

 
a branch: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
// draw branch
algo.turnLeft (60);
algo.go (70);
algo.turnRight (120);
algo.go (70);
algo.turnLeft (60);
 
 
Solution: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
// hide algo
algo.hide ();

loop (7) {
  algo.turnLeft (60);
  algo.go (70);
  algo.turnRight (120);
  algo.go (70);
  algo.turnLeft (60);

  algo.turnRight (360 / 7);
  // a total rotation divided by the number of edges
}
 


summary

Summary

In this tutorial, you have learned:

  • what is a loop ?
  • what is a block of instructions
  • how to group and repeat a block of instructions
  • how to use them to create much beautifull drawings ?

Functions used

FunctionDescription