0% found this document useful (0 votes)
4 views14 pages

Slide9 While Loop

The document explains the concept of while loops in programming, highlighting their use when the number of iterations is unknown. It provides examples, such as a temperature converter and a guessing game, to illustrate how while loops function and how to avoid infinite loops. Additionally, it discusses the break statement, which allows early termination of loops.

Uploaded by

h230549v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views14 pages

Slide9 While Loop

The document explains the concept of while loops in programming, highlighting their use when the number of iterations is unknown. It provides examples, such as a temperature converter and a guessing game, to illustrate how while loops function and how to avoid infinite loops. Additionally, it discusses the break statement, which allows early termination of loops.

Uploaded by

h230549v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

WHILE LOOP

Mr. W Manjoro
Introduction
 For Loop allows us to repeat things a
specified number of times.
 Sometimes, though, we need to repeat
something, but we don’t know in advance
exactly how many times it has to be re-
peated.
 E.g., a game keeps going until someone
wins.,
 This is a situation that would call for a
while loop.
Example 1: Temperature converter program.
 The loop will allow the user to repeatedly enter
temperatures without restarting the program.
 A simple way for the user to indicate that they
are done is to have them enter a nonsense tem-
perature like -1000.
The while statement says that we will keep loop-
ing, that is, keep getting and converting tempera-
tures, as long as the temperature entered is not -
1000.
As soon as -1000 is entered, the while loop stops.
Tracing through, the program first compares temp
to -1000.
Iftemp is not -1000, then the program asks for a
temperature and converts it.
The program then loops back up and again com-
pares temp to -1000.
 If temp is not -1000, the program will ask for
another temperature, convert it, and then loop
back up again and do another comparison.
 It continues this process until the user enters -
1000
 We need the line temp=0 at the start, as with-
out it, we would get a name error because
temp doesn’t yet exist.
 To take care of this, we just declare temp equal
to 0.
 Note that is natural to think of the while loop as
continuing looping until the user enters -1000.
 However, when we construct the condition, in-
stead of thinking about when to stop looping, we
instead need to think in terms of what has to be
true in order to keep going.
 A while loop is a lot like an if statement.
 The difference is that the indented statements in
an if block will only be executed once, whereas
the indented statements in a while loop are re-
peatedly executed.
Example 2
 One problem with the previous program
is that when the user enters in -1000 to
quit, the program still converts the value
-1000 and doesn’t give any message
to indicate that the program has
ended.
 A nicer way to do the program is shown
below.
Example 3
 When we first met if statements, we wrote
a program that played a simple random
number guessing game.
 The problem with that program is that the
player only gets one guess.
 We can, replace the if statement in that
program with a while loop to create a pro-
gram that allows the user to keep guess-
ing until they get it right.
Infinite loops
 When working with while
loops, sooner or later you
will accidentally send
Python into a never ending
loop.
 In this program, the value of
i never changes and so the
condition i<10 is always
true.
 Python will continuously
print zeroes.
The break statement
 The break statement can be used to
break out of a for or while loop before
the loop is finished
 Example 1
 Here is a program that allows the user to
enter up to 10 numbers. The user can
stop early by entering a negative num-
ber.
 Example 1 could also be accomplished
with a while loop.
• Either method is
ok.
• In many cases
the break
statement can
help make your
code easier to
understand.

You might also like