0% found this document useful (0 votes)
4 views

loop and exercices

Uploaded by

yaasinecode578
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

loop and exercices

Uploaded by

yaasinecode578
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

ITERATIVE OR REPETITIVE INSTRUCTIONS

Chapter Objective: Understand and manipulate iterative instructions and know how to compare
them.
Chapter Outline:
I. Introduction
II. Repeat Instruction
III. For Instruction
IV. While Instruction
V. Comparison of Iterative Forms

I. INTRODUCTION
Iterative structures are used to describe the repetition of an instruction or a sequence of instructions. Any repetition
of instructions, also known as a loop, must be finite and controlled using a logical expression or condition, the
change of which leads to either the termination of the repetition or the continuation of the execution of these
instructions.
There are three forms of loops: repeat loop, for loop, and while loop.

II. REPEAT INSTRUCTION


The general form of this instruction is:

Repeat
<Processes>
until <Stop_Condition>

Example
Write an algorithm to read an integer value strictly less than 100.

SOLUTION
ALGORITHM Value_Less_100
BEGIN
Val: integer
Repeat
Write("Enter a value less than 100:")
Read(val)
Until val < 100
END

Note: The repeat instruction is executed before evaluating the condition, so it is executed at least once.

Exercise
Write an algorithm named Sum to calculate the sum of the first 10 integer values starting from 1.
SOLUTION
ALGORITHM Sum
BEGIN
i, s: integer
i←1
s←0
Repeat
s←s+i
i←i+1
Until i > 10
Write("The sum is:", s)
END

III. FOR INSTRUCTION


The general form of this instruction is:
For <var> from <initial_value> to <final_value> step = <step>
Do
<Processes>
End for

Note:

• The entry condition depends on the final value. It is a relationship between the counter variable and the final
value.
• The transition to the next value of the counter depends on the step. It is a relationship between the counter
variable and the step.
• If a step value is not assigned, then by default it is 1.

Example:
Write an algorithm to display the text "Good Morning" 50 times.

SOLUTION:

ALGORITHM Greeting Useless


BEGIN
i: integer
FOR i FROM 1 TO 50 Step=1 Do
WRITE("Good Morning")
END FOR
END
Note: The for instruction is recommended if the number of repetitions is known.
Exercise:

Réécrire l'algorithme Somme avec la forme pour.


SOLUTION:
ALGORITHM Sum
BEGIN
i, s: integer
s←0
FOR i FROM 1 TO 10 DO
s←s+i
END FOR
WRITE("The sum is:", s)
END

IV. WHILE INSTRUCTION


The general form of this instruction is:

While <Entry_Condition> do

<Processes>

End while

Example:
Rewrite the Greeting algorithm using the while version.

SOLUTION
ALGORITHM Greeting
BEGIN
i: integer
i←1
WHILE i <= 50 DO
WRITE("Good Morning")
i←i+1
END WHILE
END

Note:
- The while instruction is executed only if and only if the entry condition is true.

- The while instruction cannot execute the <Processes> part if the entry condition is false from the
beginning.
Exercise
Rewrite the Sum algorithm using the while version.
SOLUTION
ALGORITHM Sum
BEGIN
i, s: integer
s←0
i←1
WHILE i <= 10 DO
s←s+i
i←i+1
END WHILE
WRITE("The sum is:", s)
END

V. COMPARISON OF REPETITIVE FORMS


To choose which form to use, it is advisable to follow the following steps:

• If the number of repetitions is known, it is preferable to use FOR.

• Otherwise, if a process needs to be executed at least once, use REPEAT.

• Otherwise, use WHILE.

The following figure summarizes the process of choosing a repetitive form:

True False
Number of repetitions
is known

True False

FOR Execute at least once

REPEAT WHILE
Exercises:

Write an algorithm that allows the user to enter the code of a product (an integer between 1 and 4) to display its
category. The possible categories are "Electronics," "Clothing," "Food," and "Toys." The algorithm should continue to
prompt for inputs as long as the user desires. If the user enters an invalid code, display "Invalid product code."

Solution:

ALGORITHM Products
BEGIN
response, code: integer
REPEAT
Write("Do you want to enter the code of a product? (1 for Yes, 0 for No): ")
Read(response)
IF response = 1 THEN
Write("Please enter the product code (an integer between 1 and 4): ")
Read(code)
SWITCH code
CASE 1: Write("Product category: Electronics")
CASE 2: Write("Product category: Clothing")
CASE 3: Write("Product category: Food")
CASE 4: Write("Product category: Toys")
OTHERWISE: Write("Invalid product code")
END SWITCH
END IF
UNTIL response <> 1
END

Write an algorithm that successively asks the user for 20 numbers and then tells them which was the largest among
these 20 numbers:
Enter number 1: 12
Enter number 2: 14
...
...
Enter number 20: 6
The largest of these numbers is: 14

Solution:

Algorithm Largest
Begin
N, i, L: integer
L←0
For i from 1 to 20 do
Write("Enter a number: ")
Read(N)
If N > L then
L←N
End if
End for
Write("The largest number was: ", L)
End

Then modify the algorithm so that the program additionally displays in which position this number was entered:
It was the number 2.
Solution:

Algorithm Largest
Begin
N, i, L, P: integer
L←0
P←0
For i from 1 to 20 do
Write("Enter a number: ")
Read(N)
If N > L then
L←N
P←i
End if
End for
Write("The largest number was: ", L)
Write("It was entered at position number ", P)
End

Rewrite the previous algorithm, but this time, we do not know in advance how many numbers the user
wants to enter. The input of numbers stops when the user enters zero.

ALGORITHM Largest
BEGIN
N, i, L, P: Integer
N←1
i←0
L←0
WHILE N <> 0 DO
WRITE("Enter a number: ")
READ(N)
i←i+1
IF N > L THEN
L←N
P←i
END IF
END WHILE
WRITE("The largest number was: ", L)
WRITE("It was entered at position number ", P)
END

Write an algorithm that allows the user to enter a positive integer N. The algorithm should display a figure
composed of lines containing a number of asterisks (*) determined by the value of N. If N is 0, the program
stops.
Example:
N=4

*
**
***
****
Solution:

ALGORITHM Asterisks
BEGIN
N, i, j: integer
REPEAT
WRITE("Enter a positive integer (0 to stop): ")
READ(N)
IF N <> 0 THEN
FOR i FROM 1 TO N DO
FOR j FROM 1 TO i DO
WRITE("* ")
END FOR
WRITE()
END FOR
END IF
UNTIL N = 0
END

Write an algorithm that asks for a starting number and calculates its factorial.
Note: The factorial of 8, denoted as 8!, is equal to 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8.

Solution:

Algorithm Factorial
Begin
N, i, F: integer
Write("Enter a number: ")
Read(N)
F←1
For i from 2 to N do
F←F*i
End for
Write("The factorial is: ", F)
End

You might also like