4 - Iteration Structures
4 - Iteration Structures
2022 - 2023
2023/2024
Plan
I. Introduction
2
I. Introduction
3
Iteration treatment
5
II. « For … do … End for » structure
6
« For … do … End for » structure
Syntax Current value
Final value
7
« For … do … End for » structure
8
« For … do … End for » structure
The For loop is used when the iterations number is known in advance
9
« For … do … End for » structure
Example 1:
For i from 1 to 5 do
for i in range(1,6):
print(i*100)
Print (i * 100)
End for
Execution: i 1 2 3 4 5
Display 100 200 300 400 500
10
« For … do … End for » structure
Example 2:
End for
Execution: i 5 4 3 2 1
Display 500 400 300 200 100
Algorithm Sum_natural_numbers
Var
N, i, sum: integer sum = 0
Begin N = int(input("Enter a natural number\n"))
sum 0 for i in range(1,N+1):
Print (‘’ Enter a natural number ‘’) sum = sum + i
Read (N)
For i from 1 to N do print("The sum of",N,"first natural numbers
sum sum + i is:",sum)
End for
Print (‘’The sum of‘’, N,’’ first
natural numbers is: ’’, sum)
End
12
« For … do … End for » structure
Example 4: Write an algorithm that reads a positive integer n and displays all of its
divisors
Algorithm Divisors
n = int(input("Enter an integer\n"))
Var for i in range(1,n+1):
n, i: integer if n % i == 0:
Begin print(i,"is a divisor of",n)
Print (‘’ Enter an integer ‘’)
Read(n)
For i from 1 to n do
if (n mod i = 0) Then
Print (i,‘’ is a divisor of‘’,n)
End if
End for
End
13
III. « While … do … End while »
14
« While … do … End while » structure
Syntax
While (condition) do while condition:
action(s)
Treatment
End while
The treatment is executed while the condition is true otherwise we exit the loop
The loop may never be executed (0 times if the condition is false from the first
iteration)
The number of repetitions is not known in advance
15
« While … do … End while » structure
16
« While … do … End while » structure
Example 1:
i1
While (i <= 5) do
i = 1
Print (i * 100) while i<=5 :
ii+1 print(i*100)
i = i + 1
End while
Execution:
i 1 2 3 4 5
Display 100 200 300 400 500
17
« While … do … End while » structure
Example 2: Write an algorithm that calculates and displays the sum of the N first natural numbers (N
is entered on the keyboard)
sum = 0
Algorithm Sum_natural_numbers i = 1
Var N = int(input("Enter a number\n"))
N, i, sum: integer while i<=N:
Begin sum = sum + i
sum 0 i = i + 1
i1 print("The sum of",N,"first numbers
Print (‘’ Enter a number ‘’) is:",sum)
Read (N)
While (i <= N) do
sum sum + i
ii+1
End while
Print (‘’The sum of‘’, N,’’first natural numbers is: ’’, sum)
End
18
« While … do … End while » structure
Example 3: Write an algorithm that calculates the factorial of a given positive integer n.
Algorithm Factorial
Var
n, i, fact: integer
Begin
fact 1 fact = 1
i1 i = 1
Print (‘’ Enter an integer ‘’) n = int(input("Enter an integer\n"))
Read(n) while i<=n:
While ( i <= n ) do fact = fact * i
i = i + 1
fact fact * i
print("The factorial of",n,"is:",fact)
ii+1
End while
Print (‘’The factorial of ‘’, n,’’is: ’’,
fact)
End 19