Professional Documents
Culture Documents
While Loop
Advantages of Looping.
Loop:
Set of instructions that is executed repetitively based on a condition.
Allows processing on large sets of data, such as complex payroll and
benefits processing.
iProduct = 2 * iProduct
Example 2
Problem: A class of ten students took a quiz. The marks (integers in the range 0 to
100) for this quiz are available to you. Determine the class average on the quiz.
IPO Diagram:
MarkCounter = MarkCounter + 1
endwhile
Average = Total / 10
Display Average
Example 2: Explanation
Three parts to the loop:
Initialise: Set MarkCounter to 1
Compare: Compare MarkCounter to 10
Body: Add Mark to Total, add 1 to MarkCounter
When MarkCounter has a value of 11, the loop ends.
This loop is executed for each mark.
Example 3
Problem: Develop a class-averaging program that processes an unknown number
of marks each time the program executes.
Example 3: Algorithm (Sentinel Controlled)
Initialise Total to zero
MarkCounter = MarkCounter + 1
endwhile
if MarkCounter != 0 then
else
Display “No marks were entered”
endif
Looping by Decrementing
Decrementing: Counting down
N 10 * N 100 * N 1000 * N
2 20 200 2000
3 30 300 3000
4 40 400 4000
5 50 500 5000
MarkCounter = 10
while MarkCounter > 0 :
Get Mark
Total = Total + Mark
MarkCounter = MarkCounter -1
endwhile
Huiswerk: