0% found this document useful (0 votes)
19 views28 pages

Understanding Repetitive Structures in Loops

Course

Uploaded by

chifaachott
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views28 pages

Understanding Repetitive Structures in Loops

Course

Uploaded by

chifaachott
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Ministry of Higher Education and Scientific Research

Djilali BOUNAAMA University - Khemis Miliana(UDBKM)


Faculty of Science and Technology
Department of Mathematics and Computer Science

Chapter 4

Iteration/Repetetive Structures :
Loops
MI-L1-UEF121 : Algorithms and Data Structures I

Noureddine AZZOUZA

1
Course
Topics
1. Introduction

2. « for » loop

3. « while » loop

4. « repeat ... until » loop

5. Nested loops

2
ADS I Noureddine AZZOUZA
Introduction

3
Iteration Structures

Problem
Iteration Structures

 Write an algorithm that displays the multiplication table of an


integer between 1 and 10

4
ADS I Noureddine AZZOUZA
Iteration Structures

Definition
Iteration Structures

 A repetitive structure is a structure that repeats the same processing as


many times as desired depending on an execution condition.
 A Repetitive Structure is also called Iterative Structure or a Loop.
 A loop consists of four essential elements :
 A statement block, which will be executed a certain number of times;
 A condition/test, which concerns at least one so-called loop variable.
 An initialization of the loop variable.
 An update/modification to the loop variable to stop the loop.

5
ADS I Noureddine AZZOUZA
Iteration Structures

Definition
Iteration Structures

 There are 3 forms of repetitive structures (loops)


1. The FOR loop
2. The WHILE loop
3. The REPEAT loop
 These structures have the same power; but by convention the choice of a
loop depends essentially on the nature of the problem to be solved
 Infinite loop: is a loop whose condition never changes, causing an infinite
number of repetitions.
 Iteration: it is a complete loop cycle including the loop block, the condition
test, and also the modification.

6
ADS I Noureddine AZZOUZA
for
loop
7
the FOR loop

the FOR loop


Iteration Structures

 the FOR loop is a repetitive structure that iterates the same processing for a range
of values between a lower bound and an upper bound. the FOR loop is used
when the number of repetitions is known in advance

Syntax
for := to do

endfor
...
8
ADS I Noureddine AZZOUZA
the FOR loop

Running for loop


Iteration Structures

1st Stage : Initialization of the Loop Variable with the Initial Value.
2nd Stage : Evaluate the test/condition and check whether the Loop Variable value
is in range or not.

 If it is in the interval then go to the 3rd Stage, otherwise go to the 5th Stage.

3rd Stage : Execution of the loop statement block.


4th Stage : Automatic update/modification (Increment) of the value of the Loop
Variable according to the value of the Step (default: Step = 1) and return to the 2nd
Stage.
5th step: Exit from the loop and continue execution of the program starting from the
first instruction which comes after endfor.
9
ADS I Noureddine AZZOUZA
the FOR loop

for loop
Flow
Iteration Structures

Diagram

10
ADS I Noureddine AZZOUZA
the FOR loop

Example1 : Multiplication table of a number


Iteration Structures

Algorithm table_multiple;
Multiplication table
Var N, i : integer ;

begin
read (N);

for i := 1 to 10 do

write (N, ‘x’, i, ‘ = ‘, N*i)

Analyse : endfor

end.

11
the FOR loop

Example2 : Power
Iteration Structures

Algorithm puissance;
Power
Var a, b : integer ;
p, i : integer;
begin
read (a, b);
p := 1;
for i := 1 to b do

p := p * a;

Analyse : endfor

write (‘la puissance =’, p)

end.

12
the FOR loop

PASCAL C
Syntaxe: Syntaxe:
Iteration Structures

FOR compt := val_initial TO val_final DO for (initialisation; condition; modification)


Begin ... End { ... }

program Exemple_pour; #include <stdio.h>

var int main (){


a, b, p, i : Integer;
int a, b, i; // déclaration
begin
ReadLn(a, b); // lecture int p = 1; // déclaration + initialisation
p := 1; // initialisation scanf("%d %d", &a, &b);

For i:= 1 to b do for (i = 1; i <= b; i++)


begin {
p := p * a; p = p * a;
end; }

WriteLn('La puissance = ', p); printf("La puissance = %d", p);

end. return 0;
13
}
while
loop
14
the WHILE loop

the WHILE loop


Iteration Structures

 the WHILE loop executes the body of the loop when the execution condition is
met; we will stop as soon as the condition is no longer verified.

Syntax

while do

endwhile
...
15
ADS I Noureddine AZZOUZA
the WHILE loop

Running While
Iteration Structures

1st step: Initialization of the Loop Variable before the loop.


Step 2: Evaluate and test the execution condition.
If it is verified then go to the 3rd step, otherwise go to the 5th step.

3rd step: Execution of the loop statements block.


4th step: Changing (updating) the value of the Loop Variable and returning to the
2nd step.
5th step: Exit from the loop and continue execution of the program starting from the
first instruction which comes after endwhile.

16
ADS I Noureddine AZZOUZA
the FOR loop

while loop
Flow
Iteration Structures

Diagram

17
ADS I Noureddine AZZOUZA
the WHILE loop

Example1 : Multiplication table of a number


Iteration Structures

Algorithm table_multiple;
Multiplication table
Var N, i, p: integer ;

begin
read (N);
i := 1;
while i <= 10 do
p := N * i
write (N, ‘x’, i, ‘ = ‘, p)
i := i + 1
Analyse : endwhile

end.

18
the WHILE loop

Exemple2 : GCD calculation


Iteration Structures

Algorithm calcul_pgcd;
GCD calculation
Var a, b, r: integer ;

begin
read (a,b);

while b <> 0 do
r := a MOD b
a := b
b := r
Analyze : endwhile
PGCD(18,12) = PGCD(12, 6) = PGCD(6, 0) = write (‘Le PCGD de a et b =’, a)
6 end.

19
the WHILE loop

PASCAL C
Syntaxe:
Syntaxe:
Iteration Structures

WHILE condition DO
while (condition) { ... }
Begin ... End

program Exemple_tq; #include <stdio.h>

var int main (){


a, b, r : Integer;
int a, b, r; // déclaration
begin scanf("%d %d",&a, &b);
ReadLn(a, b); // lecture
while (b != 0)
While b <> 0 do {
begin r = a%b;
r := a mod b; a = b;
a := b; b = r;
b := r; }
end;
printf("Le PGCD de a et b = %d", a);
WriteLn('Le PGCD de a et b est ', a);
return 0;
20
end. }
repeat
loop
21
the REPEAT loop

the REPEAT loop


Iteration Structures

 La boucle Répéter permet de rentrer dans la boucle quelque soit la condition et


réitère l’exécution jusqu'à ce que la condition soit vérifiée.

Syntax
Repeat

until
...

22
ADS I Noureddine AZZOUZA
the REPEAT loop

Running REPEAT
Iteration Structures

Step 1: Go inside the “Repeat” loop and execute the associated block.
Step 2: Update the variables involved in the stopping condition.
Step 3: Evaluate and test the stopping condition.
 If it is checked then go to the 4th step, otherwise go back to the 1st step.

Step 4 : Changing (updating) the value of the Loop Variable and returning to the 2nd
step.
Step 5 : the stopping condition having been reached, we exit the Repeat loop and
continue the execution of the program from the first instruction which comes after
Until.

23
ADS I Noureddine AZZOUZA
the REPEAT loop

REPEAT
loop Flow
Iteration Structures

Diagram

24
ADS I Noureddine AZZOUZA
the REPEAT loop

Example1 : Multiplication Table


Iteration Structures

Algorithm table_multiple;
Multiplication table
Var N, i, p: integer ;

begin
read (N);
i := 1;
Repeat
p := N * i
write (N, ‘x’, i, ‘ = ‘, p)
i := i + 1
Analyse : Until (i > 10)

end.

25
the REPEAT loop

Example2 : GCD calculation


Iteration Structures

Algorithm calcul_pgcd;
GCD calculation
Var a, b, r: integer ;

begin
read (a,b);

Repeat
r := a MOD b
a := b
b := r
Analyse : Until (b = 0)
PGCD(18,12) = PGCD(12, 6) = PGCD(6, 0) = write (‘Le PCGD de a et b =’, a)
6 end.

26
the REPEAT loop

PASCAL C
Syntaxe:
Syntaxe:
Iteration Structures

REPEAT bloc UNTIL (condition)


do { ... } while (condition)

program Exemple_repeter; #include <stdio.h>

var int main (){


a, b, r : Integer; int a, b, r; // déclaration
scanf("%d %d",&a, &b);
begin
ReadLn(a, b); // lecture do
{
repeat r = a%b;
r := a mod b; a = b;
a := b; b = r;
b := r; }
until (b = 0); while (b != 0);

WriteLn('Le PGCD de a et b est ', a); printf("Le PGCD de a et b = %d", a);

end. return 0;
27
}
Ministry of Higher Education and Scientific Research
Djilali BOUNAAMA University - Khemis Miliana(UDBKM)
Faculty of Science and Technology
Department of Mathematics and Computer Science

Chapter 4

Iteration/Repetetive Structures :
Loops
MI-L1-UEF121 : Algorithms and Data Structures I

Noureddine AZZOUZA

28

You might also like