Chap4-Loop Statements
Chap4-Loop Statements
Faculty of Sciences
Department of Mathematics and Computer Science
Year: 1st Year Computer Science Licence Degree
(L.M.D)
2024-2025
1
Chapter IV
Loop Statements
2
Objective
• Understand the need to use repetitive statement
• Learn the different loop statements
• Analyze problems and write algorithms using loop
statement
• Write programs in C language using loop statement
3
Definition
The Loop statement is a control flow statement that execute one or more
instructions , called loop’s body, repeatedly multiple times.
• When you need to execute a block of code multiple times then you need to use
looping concept
4
Principle
The Condition is evaluated :
6
Types of Loops
We introduce 3 kinds of loop statements which belong to 2 main types :
❑ Pre-test loops: the condition is evaluated before the loop body is executed. If the
condition is false, the loop terminates without executing the loop body.
1. While
2. For
❑ Post-test loops: the condition is evaluated after the loop body is executed. If the
condition is false, the loop terminates.
3. Do .. While 7
Usage of loop statement
Usage of a loop type depends on knowing in advance the repeat count
9
1. For loop
• Repeat count is known in advance: In this structure, the exit
from the iteration loop occurs when the desired repeat
count is reached.
Syntax : Initialization
positive or negative
integer
10
1. For loop
• If Initial value < Final value, then the loop is called ‘Ascending,' and the
iteration step will be positive (n>0)
• If Initial value > Final value, then it is called ‘Descending,' and the iteration
step will be negative (n<0)
11
1. For loop
❑ For each index variable (or iterator) value that varies from the initial value
to the final value the block will be executed.
❑ Each execution of the block is called an iteration.
❑ initialization of the index variable is done first
❑ In each iteration the loop performs :
✓ Execution of the instructions block
✓ Increment (n>0) or decrement (n<0) of the index
12
1. For loop
Examples
//ascending loop
For i 0 To 10 Step 2 Do
Write (i) ; Result : 0 2 4 6 8 10
//descending loop
For j 10 To 1 Step -1 Do
Write (j); Result : 10 9 8 7 6 5 4 3 2 1
//block execution
For k 0 To 20 Step 5 Do
Begin
Write (k);
Result : 0 0 5 50 10 100 15 150 20 200
Write(k*10);
13
end;
1. For loop
• In case where the step is 1 (n=1), the general form of the For loop will
be:
For Index Initial_value To Final_value do
Action ;
Example
For i 1 To 10 Do
Write (i) ; Result : 1 2 3 4 5 6 7 8 9 10
14
2.While loop
15
2.While loop
Action
First check the condition, if condition is true then
control goes inside the loop body. Otherwise goes
outside the loop body. Loop Update
16
2.While loop
17
2.While loop
18
2.While loop
Analysis Algorithm sub;
Var
1.Read the value of an integer x x, cnt: integer;
Begin
2.Initialize the subtraction counter cnt
Read (x);
to 0
cnt 0;
3.If x≥0 then substruct 11 from x and While (x>=0) do
increment the number of subtraction begin
cnt by 1. x x-11;
cnt cnt +1;
4.if x≥0 then go to (3) else exit loop end ;
5.Display value of cnt Write (″The number of subtraction done is :″, cnt);
End.
19
3.Do .. While loop
20
3. Do .. While
21
3. Do .. While
Syntax :
Action
Do
Action ;
While Condition ; False True
Condition
22
3. Do .. While
Action
• Note1: The body of the Do while loop may at
least be executed once. So, this structure
actually allows repeating an action 1, 2 or Loop update
multiple times.
23
3. Do .. While
Exercice: Do the trace (sequence) for Algorithm subt;
✓ x = 51. Var
• Example : Previous exercise x, cnt: integer ;
✓ x=-3
Design the Flow Diagram Begin
Read (x);
cnt 0;
Do
if x>=0 Then cnt cnt +1;
x x-11;
While x>= 0 ;
Write (‘The number of subtraction done is :’, cnt);
End.
24
3. Do .. While
Exercise
Write an algorithm that allows to read the value of an integer n , knowing
that n must be less than or equal 100.
Algorithm N_value;
Var
n: integer ;
Begin
Do
Read (n);
While n > 100 ;
Write (n);
End.
25
4. For and While loop
28
4. Nested loop
All types of loops can be nested inside each other
Example 4 Trace
i j Result For i 1 to 3 do
For i 1 to 3 do 1 1 1 begin
2 2 j1;
For j 1 to 2 do
3 while j <= 2 do
Write (i*j); 2 1 2 begin
2 4
Write (i*j);
3
j j+1;
3 1 3
end;
2 6
3
End;
4 29
Translation into Language
30
Translate For loop in C
identical
31
Translate For loop in C
32
Translate While loop in C
33
Translate Do..while in C
34
Exercises with solutions
35
Exercises
Algorithm display_numbers;
Var i : integer ;
Begin
For i 1 To 100 do
Write (i);
End .
37
Solutions
Exercise 2
Write an algorithm that finds the sum of all the even numbers from 1 to 100, using a while
loop.
Algorithm sum;
Var number , sum: integer ;
Begin
number 1;
sum 0;
While number <= 100 do
begin
if (number mod 2) =0 then sum sum + number;
number number + 1;
end;
Write (“The sum of even numbers from 1 to 100 is “, sum);
End .
38
Solutions
Exercise 3
Write an algorithm that allows to read the value of the integer n , knowing that n must
be greater than 100.
Algorithm numbers;
Var n: integer ;
Begin
Do
read(n);
While (n<=100);
End .
39
Additional exercises
4. Write an algorithm that checks if a natural number a is prime or not.
(A prime number is divisible only by 1 and itself).
5. Write an algorithm that calculates the GCD (Greater Common Divisor) of
two given numbers. E.g. GCD of 18 and 12 is 6
• Tip : repeat subtracting the smaller number from the larger number
until they equal.