L06 - Flow Control - While Loop and Basic For Loop
L06 - Flow Control - While Loop and Basic For Loop
Semester 1, 2021
Lecture 6,
Control Flow - While Loop and Basic For Loop
COMMONWEALTH OF AUSTRALIA
WARNING
2
Week 4: Control Flow - While Loop
We will cover: While loop, including nested while loop. Basic for loop
through a range of numbers.
You should read: §§1.3 of Sedgewick
3
Previously in INFO1110...if...else i f
speed false
<= 60
true
speed false
fine = 0
<= 70
true false
true
4
Previously on INFO1110...if...else i f
speed false
<= 60
true
speed false
fine = 0
<= 70
true false
true
5
Lecture 6: While Loops and For Loops
6
while
Steps of while :
1 while (condition) do
2 · statement
7
while loop diagram
false 1
begin with the condition
condition 2
Check the condition
3
if it’s true, execute the statement;
true otherwise, exit the loop
4
go back to the condition
statement(s)
8
while loop example
i=0
i
false
condition false
i <5
true
true
statement(s)
i=i+1
9
while loop diagram to code
i=2
code?
false
i < 10
true
i=i-1
i=i*5
10
while loop desk checking
i=2
false
i < 10
true
i=i-1
i=i*5
11
while loop desk checking
12
while loop desk check
distance = 0.0
distance walking
walking = true
0 T
walking false
true
distance =
distance + 10
distance false
> 42.5
true
walking = false
13
while
Steps of while:
1 while (condition) do
2 · statement
Python Syntax:
while condition :
statement
14
while example
15
while ( t r u e )
1 number = 1
2 while True:
3 p r i n t ( number)
4 number = number + 1
16
break
false
condition
• break is a special reserved word
• When used in loops, it means
“break from the current iteration of true
optional
this loop”
statements…
• It isn’t allowed in an if statement.
break
optional
statements…
17
break example
distance = 0.0
false
true
true
distance =
distance + 10
distance false
> 72.5
true
break
18
continue
false
• continue is a special reserved word condition
• When used in loops, it means “skip the
rest of this iteration and go on to the true
next one”
optional
• It is only allowed inside the body of a statements…
loop.
continue
optional
statements…
19
while loop with break and continue
20
Code trace: break and continue — example
1 x=4
2 y = 20
3 while x < y:
4 x += 1
5 if x % 2 == 0:
6 continue
7 print(" x = " + str( x))
8 if x == 13:
9 break
10 print(" All done!")
21
1
x=4 ~> python Continuing.py
2
y = 20 x =5
3 while x < y: x =7
4 x += 1
x =9
5 if x % 2 == 0:
6 continue
x = 11
7 print(" x = " + str( x)) x = 13
8 if x == 13: A l l done!
9 break
10 print(" All done!")
22
Nested loopexample
true
friends =
friends + 1
digits = 0
true
digit = digit + 1
23
Nested loopexample
friends = 1
true
friends =
friends + 1
digits = 0
true
digit = digit + 1
24
Basic For Loops
While loop revisited
while loop is simple and good
only need to consider the condition, when the loop keeps going or stops
other code can contribute to the setup and change in the loop condition
e.g. a counter variable initialised, checked, updated, checked, updated,
checked, etc.
1 # initilise
2 counter = 0
3 while counter < 10:
4 # do something in each iteration
5 ...
6 # move toward the end of loop condition
7 counter = counter + 1;
26
for
Know your while loops!
Syntax:
E.g. for i in range (6) :
27
for (cont.)
28
range
1 f o r num i n r a n g e ( 5 ) :
2 p r i n t ( " t h e v a l u e o f num i s " + s t r ( num ) )
29
range (cont.)
range(start, s t o p , step)
30
range (cont.)
range(start, s t o p , step)
31
for vs while
Which do I choose?
How many iterations do you need? if you know the answer to this
question in advance, a for loop is suitable
32