Chapitre 3 - Loops
Chapitre 3 - Loops
While loop is used to execute the body of the loop until a specific condition
is false. We apply this loop when we don't know how many times it will run.
The loop while consists of a loop condition, a code block as loop body and
a loop update expression. First, the loop condition is evaluated, and if true,
the code in the body of the loop will be executed. This process repeats until
the loop condition becomes false.
The loop while
Start
true
Condition
false Statements
Exit
Example of using the While loop
Count from 10 to 20
algorithm count;
var i:integer;
begin
i10; //initialization
While(i<=20) DO//the number 20 is included
begin
write(i);
ii+1; //we increment the counter i (variation of the
//condition and preparation of the next //iteration)
END;
END.
The do-while loop
The do-while loop is a control flow instruction that executes a block of code
at least once, then repeatedly executes the block, or not, based on a
Boolean condition given at the end of the block.
In the control structure do-while, there are three main elements which are:
Action
Updating loop condition
Loop Condition Assessment
The do-while loop
Statements
true
Condition
false
Exit
Example of using the While-do loop
Count from 20 to 10
In C language In Algorithmics
algorithm countv2;
var i:integer;
begin
i20;//initialization
do
begin
write(i);
ii-1;// variation of the condition
end;
While (i>=10) //the number 10 is included
end.
The loop for
The FOR loop is preferable to previous loops when the number of times a
block of instructions is to be executed is known in advance. The main use of
the loop FOR is to manage a counter (integer type)
The for loop is preferred when:
• The variation interval is known
• The increment step is fixed
• Loop exit is related to the loop index
On the other hand, we use the 'while' loop when the condition of the loop
is:
• Composed of complex logical expression,
• Evaluates additional variables other than the loop counter,
Loop syntax for
Start
for(int i = 0;i<10;i++)
i=0 {
statements;
}
true
Condition
(i<10)
Statements
false
i=i+1
Exit
Example of using the loop for
Count from 10 to 20
In C language In Algorithmics
algorithm countv2; Initial counter value
var i:integer;
begin
for i 10 to 20 do
begin
write(i); Condition
end;
end.
The main instruction and
automatic increment of i
The 'repeat' loop
Count from 20 to 10
In Algorithmics
ii+1; ii+1;
end;
end;
end. end;
while(i <=20)
end. end.
Initial counter
value = 10 (lower
limit) Step = 1
Navigate a range in descending order
algorithm count;
var i,sum:integer; Need to traverse the
rank [1,10]
begin
Initial value =0; the 0
does not disturb the sum0;
addition operation
i 1;
while (i<=10) do
begin
sumsum+i;
ii+1;
end;
write(sum);
end.
The table detailing the execution flow
of loops
The loop execution flow table is necessary to see the evolution of the
variables manipulated inside the loops as well as the correct checking of
the condition and the initialization of the value of the loop counter. This
table is composed of n lines for n iterations executed by the loop, plus the
initial state of the variables before launching the loop. In columns, we find
the iteration number, the variables manipulated inside the loop, and the
output if applicable.
Examples of using the loop execution
table
Sum of the first 5 numbers
algorithm count;
var i,sum:integer;
begin
iteration i sum output
sum0;
i 1; initialization 0 0 -
while(i<=5)do 1 1 1 1
begin 2 2 3 13
sumsum+i;
3 3 6 136
write(sum+ " ");
ii+1;
4 4 10 1 3 6 10
end; 5 5 15 1 3 6 10 15
end.
Infinite loops
The problem of infinite loops occurs when there is an error in the condition
of the loop, or in the variation of the condition, below are the possible
causes of an infinite loop:
The counter is not varied (incremented or decremented), this is not the case of
the loop for because the variation of the counter is done automatically.
Vary the loop counter so as to never check the stopping condition, in this case, it
is necessary to clearly specify the range of the counter, its limits, and the
movement of the counter (increment or decrement).
Vary the loop counter inside a conditional statement.
Wrong condition.
Infinite loops
To avoid the infinite loop problem, you must take the following precautions:
Carefully study the range of variation of the counter.
Specify the loop condition carefully by taking into account the extreme values of
the counter.
Involve the counter in the loop condition and don't forget to vary it.
Examples of infinite loops
A nested loop means a loop inside another loop. We can have any
number of loops inside another loop.
Example:
For i0 to 10 do
for j 0 to 5 do
statements;
References