Professional Documents
Culture Documents
Problems Solving With Loops:: WHILE DO Statement
Problems Solving With Loops:: WHILE DO Statement
WHILE…DO Statement:
The first of the three types of loop structures is the While/Do structure. This type of loop
tells the computer that while the condition is True, repeat all instructions between the While and
the WhileEnd. The form of the algorithm is the following:
while (condition) do
action
23
Algorithm Design and Programming Techniques Lecture 3
1st class Asst. Lecture Omar Nowfal
Note:
Be careful about infinite loop. The action within the loop must modify variables in the condition.
Example 1: Write an algorithm that computes the sum of consecutive integer numbers 1 + 2 +
3 +….. + n.
Algorithm Sum_Consecutive ( )
Begin
input (n)
sum ← 0
i←1
while (i <= n) do
sum ← sum + i
i←i+1
output (sum)
End
Example 2: Write an algorithm that computes the sum of ten numbers input by the user. Use
while loop.
Algorithm Summation ( )
Begin
sum ← 0
i←1
while (i <= 10) do
input (Num)
sum ← Sum + Num
output (sum)
End
24
Algorithm Design and Programming Techniques Lecture 3
1st class Asst. Lecture Omar Nowfal
REPEAT…UNTIL Statement
The second type of loop structure is the Repeat/Until structure. This type of loop tells the
computer to repeat the set of instructions between the Repeat and the Until, until a condition is
True. There are two major differences between this instruction and the While/Do. First, in the
While/Do loop, the program continues to loop as long as the resultant of the condition is True;
in the Repeat/Until loop, the program stops the loop process when the resultant of the condition
is True. Second, in the While/Do loop, the condition is processed at the beginning; in the
Repeat/Until loop, the condition is processed at the end. When the condition is processed at the
end, the instructions in the loop are processed entirely at least once, regardless of whether it is
necessary.
The general form of Repeat…Until instruction is:
repeat
action
until (condition)
25
Algorithm Design and Programming Techniques Lecture 3
1st class Asst. Lecture Omar Nowfal
Algorithm sum_consecutive ( )
Begin
input (n)
sum ← 0
i←1
repeat
sum ← sum + i
i←i+1
until (i > n)
output (sum)
End
Example 4: Write an algorithm that computes the sum of integer numbers input by the user.
The algorithm should stop when the user enters zero.
Algorithm summation ( )
Begin
sum ← 0
repeat
input (Num)
sum ← sum + Num
until (Num=0)
output (sum)
End
26
Algorithm Design and Programming Techniques Lecture 3
1st class Asst. Lecture Omar Nowfal
For Loop can represent the Automatic Counter loop, and this can be written as:
27
Algorithm Design and Programming Techniques Lecture 3
1st class Asst. Lecture Omar Nowfal
Examples:
1. Increasing by 1
for i ← 1 to 100 do
2. Decreasing by 1
for k ← 100 downto 1 do
3. Increasing by 7
for m ← 7 to 77 by 7 do
4. Decreasing by 5
for h ← 20 downto 2 by 5 do
Example 6: Write an algorithm that computes the sum of ten integer numbers input by the user.
Use for loop.
Algorithm summation ( )
Begin
sum ← 0
for I ← 1 to 10 do
input (Num)
sum ← sum + Num
output (sum)
End
28
Algorithm Design and Programming Techniques Lecture 3
1st class Asst. Lecture Omar Nowfal
29
Algorithm Design and Programming Techniques Lecture 3
1st class Asst. Lecture Omar Nowfal
Example:
Algorithm StopAt5 ( )
Begin
for i ← 1 to 10 do
if (i = 5) then
break
output (i)
output (“Broke out of loop at i = “ , i )
End
Example:
Algorithm Skip#5 ( )
Begin
for i ← 1 to 10 do
if (i = 5) then
continue
output ( i )
End
30
Algorithm Design and Programming Techniques Lecture 3
1st class Asst. Lecture Omar Nowfal
Homework:
1. Write algorithms to print the following shapes:
31
Algorithm Design and Programming Techniques Lecture 3
1st class Asst. Lecture Omar Nowfal
2. Write an algorithm that computes the sum of integer numbers divisible by 6 that are from 20
to 100.
3. Write an algorithm that computes the power of an integer number.
4. Write an algorithm that computes the following equation:
6. An integer number is said to be a prime if it is divisible only by 1 and itself. Write an algorithm
that determines if a number is a prime and use this program to determine and print all the
prime numbers between 10 and 30.
32
Algorithm Design and Programming Techniques Lecture 3
1st class Asst. Lecture Omar Nowfal
7. Assume j=0, what is the new value of j at the end of each of the following loops?
for i ← 8 downto 0 by 3 do
j←j+1
for i ← 0 to 8 by 2 do
j←j+1
i←i+1
8. If (i=0) and (g=5), what are the new values of i and g after the following algorithm segment?
while ((i <= 4) AND (g > 0) do
i←i+1
g←g–1
33