Understanding While Statements and Iteration in Python
This guide introduces the while statement in Python, explaining its syntax, structure, and practical applications. You will learn how to use while loops for count-controlled iterations, create conditions using and, or, and not operators, and handle user input using JES functions. The text emphasizes the importance of correct loop design to avoid common pitfalls like off-by-one errors and infinite loops. Tips are provided for testing loops and ensuring robust program behavior. Mastering while statements is essential for efficient programming in Python.
Understanding While Statements and Iteration in Python
E N D
Presentation Transcript
Computer Science 101 While Statement
T Cond F Iteration: The While-Statement • The syntax for the While-Statement iswhile <condition> : <list of statements> • Note the colon and indentation
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 printNowthat displays its output immediately. It is of the formprintNow(<whatever>)Note that it only prints one thing.
str • stris 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.
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) : // Executes 10 passes <do something> counter = counter + 1 counter = 1 while (counter < 10) : // Executes 9 passes <do something> counter = counter + 1; counter = 1 while (counter <= 10) : // Executes 10 passes <do something> counter = counter + 1 counter = 0 while (counter <= 10) : // Executes 11 passes <do something> counter = counter + 1
Infinite Loop counter = 1 while (counter <= 10) : // Executes 5 passes <do something> counter = counter + 2 counter = 1 while (counter != 10) : // Runs forever <do something> counter = counter + 2; 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
Cows just have two toes you know!