0% found this document useful (0 votes)
12 views19 pages

4 - Iteration Structures

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)
12 views19 pages

4 - Iteration Structures

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/ 19

Module: ADSP1 1st year LBC

2022 - 2023
2023/2024
Plan
I. Introduction

II. « For … do … End for » structure

III. « While … do … End while » structure

2
I. Introduction

3
Iteration treatment

Execute an action (or a set of actions) a certain number of times

The loop concept


or
Iteration structure
(or repetition statement)
4
Iteration statement
 Iteration statement treatments

1) « For … do … End for » structure

2) « While … do … End while » structure

5
II. « For … do … End for » structure

6
« For … do … End for » structure
 Syntax Current value
Final value

For cv from iv to fv (step sv) do

action(s) Initial value for cv in range(iv,fv±1,sv):


action(s)
…. ………
End for

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

 The treatment is repeated (fv – iv + 1) times

 cv is incremented automatically for each treatment execution

 The loop incrementation value is called the loop step

 The loop execution stops when cv reaches fv

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:

For i from 5 to 1 (step -1) do


for i in range(5,0,-1):
Print (i * 100) print(i*100)

End for

Execution: i 5 4 3 2 1
Display 500 400 300 200 100

→ If the step is different from 1, ( step constant) must be added


11
« For … do … End for » structure
Example 3: Write an algorithm that calculates and displays the sum the first N natural
numbers (N is entered on the keyboard)

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:
i1
While (i <= 5) do
i = 1
Print (i * 100) while i<=5 :
ii+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
i1 print("The sum of",N,"first numbers
Print (‘’ Enter a number ‘’) is:",sum)
Read (N)
While (i <= N) do
sum  sum + i
ii+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
i1 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)
ii+1
End while
Print (‘’The factorial of ‘’, n,’’is: ’’,
fact)
End 19

You might also like