Python While Statement

Download Report

Transcript Python While Statement

Computer Science 101
While Statement
Iteration: The While-Statement

The syntax for the WhileStatement is
Cond
while <condition> :
<list of statements>
F

Note the colon and
indentation
T
Example: Print first 10 positive integers
Example: Print even positives thru 20
Example: Print even positives thru 20
Function rollDie
Function rollDie (cont.)
Function rollDie (cont.)
Function checkDie
JES Input Functions
The Python input function works only for
numerical input.
 JES provides some special input functions.
These functions allow us to get input for
other types, via a dialog box that pops up
with given prompt.

– requestInteger for integers
– requestNumber for floats
– requestString for strings
JES printNow

With JES, print statements within a function do
not display their output until the function has
completed its execution. Sometimes this is ok,
but often the user needs to see intermediate
results for purposes of decision making. JES
provides a function printNow that displays its
output immediately. It is of the form
printNow(<whatever>)
Note that it only prints one thing.
str

str is a Python function that will convert a number
to a string.
This is often useful when we want to combine
string and numerical data for output.
Recommendation

Unless specified otherwise, you should
use the “request” forms for input and
printNow for output in your functions.
Function averageScores
More on Conditions

and
In Python we can
combine two conditions
with and to form a new
condition that is true
only if both or the
original conditions are
true.
More on Conditions

or
In Python we can
combine two conditions
with or to form a new
condition that is true
provided at least one of
the original conditions is
true.
More on Conditions

not
In Python we can
make a new condition
from a given condition
by placing not in front of
the original condition.
The truth value of the
new condition is the
opposite of the original
condition.
Count-controlled Loops
// General form
<initialize a counter variable>
while <test counter for termination condition> :
<do something>
<change the value of counter>
Count-controlled Loops
// General form
<initialize a counter variable>
while <test counter for termination condition> :
<do something>
<change the value of counter>
// Count up
<initialize a counter variable>
while <counter is less than a limit value> :
<do something>
<increase the value of counter>
counter = 1
while counter <= 10 :
<do something>
counter = counter + 1
Count-controlled Loops
// General form
<initialize a counter variable>
while <test counter for termination condition> :
<do something>
<change the value of counter>
// Count down
<initialize a counter variable>
while <counter is greater than a limit value>:
<do something>
<decrease the value of counter>
counter = 10
while (counter > 0):
<do something>
counter = counter - 1
Increment and Decrement
counter = 1
while counter <= 10 :
<do something>
counter += 1
counter = 10
while counter > 0 :
<do something>
counter -= 1
Designing Correct Loops

Initialize all variables properly
– Plan how many iterations, then set the
counter and the limit accordingly

Check the logic of the termination
condition

Update the loop control variable
properly
Off-by-One Error
counter = 0
while (counter < 10) :
<do something>
counter = counter + 1
// Executes 10 passes
counter = 1
while (counter < 10) :
// Executes 9 passes
<do something>
counter = counter + 1;
counter = 1
while (counter <= 10) :
<do something>
counter = counter + 1
counter = 0
while (counter <= 10) :
<do something>
counter = counter + 1
// Executes 10 passes
// Executes 11 passes
Infinite Loop
counter = 1
while (counter <= 10) :
<do something>
counter = counter + 2
counter = 1
while (counter != 10) :
<do something>
counter = counter + 2;
// Executes 5 passes
// Runs forever
In general, avoid using != in loop termination
conditions with count-controlled loops.
Testing Loops

Can vary the limit or the control
variable, or both

Use a negative value, zero, and a
positive value

Display a trace if things aren’t working