Loops in Python
Repetition Loops in Python
Python has two loop structures, the for loop and
the while loop.
The for loop is usually used where the number of
repetitions are known.
The while loop is usually used where repetitive operations
are required for an unknown number of times.
The structure of a for loop is as follows
statement in main program
...............
...............
statement in main program
for loopCounter in range( start, stop, step ) :
statement in loop
...............
...............
statement in loop
statement in main program
statement in main program
operation of a for loop
loopCounter = start
Execute loop
statements
loopCounter = loopCounter + step
yes
Is loopCounter < stop?
no
Look at the first control line of the for loop
for loopCounter in range( start, stop, step ) :
loop counter start value of Step amount of
which is used to the loop the loop counter
control the loop counter between loops.
(optional)
stop value of the loop counter.
The stop value is not used
The variables loopCounter, start, stop and step must all
be integers.
Here is an example program using a for loop
Here is the output to that program
Here is the same program executes a loop starting at 8, and stepping
by -2, until the loop counter reaches 2.
Notice it does not do the loop when the loop counter is 2
Here is another program using a for loop where the step integer is
omitted. In which case a step of 1 is assumed
Here is the output. Notice the stop number is not output
Python does numbers start to stop-1
The for loop also works with sequences using the in operator,
As shown here using an assorted list
The variable name takes on each item in the list in turn and uses it in
a separate go of the loop. Here is the output
The structure of a while loop is as follows
statement in main program
...............
...............
statement in main program
while logical-statement :
statement in loop
...............
...............
statement in loop
statement in main program
statement in main program
operation of a while loop
is logical-statement True? no
yes
Execute loop
statements
Here are some examples of logical statements
number <= 10
17 > maximum
high >= low
left != right
name == “John”
A logical statement evaluates as ether True or False
Looking at the flow diagram of the while loop you
You should notice
If the logical-statement is not True when the while loop is
first encountered, then it is never done. In this example the
programmer has forgotten to initialise the variable number
before using it in the while loop
If the variable number had never been used before in the
program an error would be generated.
If something is not done within the loop statements to
change the logical-statement to False, then the while loop
will go on forever. For example in this program the variable
number remains at 0, and no amount of iterations of the
while loop will change that.
Often when using while loops they either never run, or
they go on forever and the program stalls – so be careful
Here is another demonstration of the while loop
This while structure will continue to prompt for a number
until a positive number is entered. If the number entered is
negative then the prompting statement will be executed
again, and keep repeating until a positive number is
entered.
Notice I preset the number variable to be -1 before
entering the loop. Otherwise the loop will not happen.
Here is a better demonstration of that program
And an example of the output
Here is a program to read in a number between 10 and 20
And an example of the output
Here is a program to add numbers, until a negative number
is input. Notice the use of an infinite loop and the break
operator
And here is an example of its output