Vous êtes sur la page 1sur 11

Algorithm Design and Programming Techniques Lecture 3

1st class Asst. Lecture Omar Nowfal

Problems Solving with Loops:


A third logic structure for designing decisions is the loop structure. The loop logic
structure is the repeating structure. Most problems in business involve doing the same task over
and over for different sets of data, so this structure is extremely important. Through the loop
structure, a program can process many payroll records or inventory items, or put a mailing list
in alphabetical or zip code order. This structure is harder to understand than the decision
structure, but easier to use. The main difficulty in using it as part of a solution is identifying
what instructions should be repeated. Besides being used to repeat instructions in a solution, the
loop structure is also used to return the processing to an earlier point in the solution. When it is
used in this way, it is a replacement for the GoTo statement. (The GoTo is an instruction that
tells the computer to transfer to another instruction in the solution instead of processing the next
instruction in sequence.)
There are many type of iterators (loops), but we concentrate at the following instruction:
1. While… Do
2. Repeat…..Until
3. Automatic-Counter Loop (For loop)

 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

Example 3: Consecutive integer numbers 1 + 2 + 3 +… + n. Use repeat/until loop.

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

 Automatic Counter Loop (FOR Loop) Instruction:


The third type of loop structure is the automatic-counter loop. This type of loop increments
or decrements a variable each time the loop is repeated. To design an automatic-counter loop,
the programmer uses a variable as a counter that starts counting at a specified number and
increments the variable each time the loop is processed. The amount to be incremented is
specified by the instruction. The set of instructions within the loop repeats until the counter is
greater than an ending number. The beginning value, the ending value, and the increment value
may be constants, variables, or expressions (calculations). They should not be changed during
the processing of the instructions in the loop. They may be changed after the loop is completed.
The test for whether to process the loop instructions in an automatic-counter loop will be found
at the beginning or at the end of the loop depending on the language or the version of the
language.

For Loop can represent the Automatic Counter loop, and this can be written as:

for initialization to/downto expression, condition by change do


action

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 5: Write an algorithm that prints the numbers from 1 to 20.


Algorithm Print ( )
Begin
for I ← to 20 do
output (I)
End

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

Example 7: Write an algorithm that computes the factorial of an integer number.


Algorithm factorial ( )
Begin
fact ← 1
input (Num)
for k ← Num downto 2 do
fact ← fact * k
output (fact)
End

 Nested FOR Statements


Loops can be nested like decisions can. Each loop must be nested inside the loop just outside
it. The general rules regarding loops, such as where the condition is processed and how
indentation and brackets are used, hold true for nested loops as well as single loops. The outer
loop increment it counter after the inner loop finished all iteration.
Example: What the output of the following instructions?
output (“i j”)
for i ← 1 to 3 do
for j ← 1 to 3 do
output( i ,” ”, j )

The output is:


I J
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

29
Algorithm Design and Programming Techniques Lecture 3
1st class Asst. Lecture Omar Nowfal

Example 8: Write an algorithm that prints the multiplication table.


Algorithm MultiplicationTable ( )
Begin
for i ← to 10 do
for j ← to 10 do
output (i, “*”, j, “= “, i*j)
End

 Break and Continue


The break statement is used to exit immediately from the loop in which it is contained.
The continue statement is used to skip the remaining statements in the body of the loop and
then continue with the next iteration of the loop.

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:

5. What is the output of the following algorithm?


Algorithm Display( )
Begin
for k ← 7 to 16 do
switch (k mod 10)
case 0: output (", ") break
case 1: output ("OFTEN ") break
case 2:
case 8: output ("IS ") break
case 3: output ("NOT ") break
case 4:
case 9: output ("DONE ") break
case 5: output ("WELL ") break
case 6: output (". ") break
case 7: output ("WHAT ") break
default: output ("Bad number.")
End

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

9. Replace the following for loop with a corresponding while loop.


a←8
for i ← 0 to 9 do
if (a = 0) then
continue
output (1/a)
a←a–1

33