You are on page 1of 6

Python: Les 6

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.

while Repetition Structure


 An action is to be repeated.
 Continues while statement is true.
 Ends when statement is false.
 Contain either a line or a body of code.
 Must alter conditional value to prevent endless loop.

Flowchart: while Repetition Structure

2011/08/01 1|Page Universiteit van die Vrystaat


Three steps in every loop:
1. Initialise a control variable.
2. Compare the control variable to a value to determine if the loop should
continue.
3. Alter the control variable within the loop

 Loop control variable: Determines whether a loop will continue to execute.


 Sentinel value: A limit or ending value to compare with the loop control
variable.
 Loop body: Statements inside the loop that are executed repetitively.
 Once the loop body is entered, the entire loop body must execute.
 Can exit from a structured loop only at the comparison test of the loop
control variable.

PythonFormat of while structure


while condition :
//Body of the while
//One or more statements

Example 1: while Python code


Problem: Display the first power of 2 that is greater than 1000.
iProduct = int(2)

while iProduct <= 1000 :

iProduct = 2 * iProduct

print

(iProduct + " is the first power

of 2 greater than 1000")

2011/08/01 2|Page Universiteit van die Vrystaat


Using a Counter to Control Looping
 Counter: Numeric variable that counts how often an event occurs.
 Incrementing: Adding to a variable, usually by 1
 Used to enter data one value at a time.
 Constant amount
 Counter used to control the number of times a set of statements will
execute.
 When counter hits certain value, the loop terminates. (Loop continues
executing until the condition is no longer met.)
 Number of repetitions is known before the loop begins executing.

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:

Input Processing Output

Mark1, Mark2, Mark3, Get Mark1, …, Mark10 Average


Mark4, Mark5, Mark6,
Calculate Total of 10 Marks
Mark7, Mark8, Mark9,
Mark10 Calculate Average of Marks

Example 2: Algorithm (Counter Controlled)


Set Total to zero

2011/08/01 3|Page Universiteit van die Vrystaat


Set MarkCounter to 1

while MarkCounter <= 10

Prompt and Get next Mark


Total = Total + Mark

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.

Sentinel Controlled Repetition


 Continues an unknown amount of times.
 Sentinel value:
o Causes loop to break.

2011/08/01 4|Page Universiteit van die Vrystaat


o Choose a sentinel value that can not be confused with an acceptable
input value.
 Number of repetitions is NOT known before the loop begins executing.
 Get first input value from the user BEFORE the while structure is executed
for the first time.

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

Initialise MarkCounter to zero

Prompt and Get first Mark (possibly the sentinel)

while (the user has not as yet entered the sentinel )

Total = Total + Mark

MarkCounter = MarkCounter + 1

Prompt and Get next Mark (possibly the sentinel)

endwhile

if MarkCounter != 0 then

Average = Total / MarkCounter


Display Average

else
Display “No marks were entered”

endif

Looping by Decrementing
 Decrementing: Counting down

 May be more convenient to control a loop by decrementing.

N 10 * N 100 * N 1000 * N

2011/08/01 5|Page Universiteit van die Vrystaat


1 10 100 1000

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:

Problem: Write a Python application that uses looping to print the


following table of values

2011/08/01 6|Page Universiteit van die Vrystaat

You might also like