Computer Science 101 While Statement
The While-Statement T F Cond The syntax for the While-Statement is while <condition> : <list of statements> Note the colon and indentation
While statement example In this example, we want a program where the user inputs an upper limit. Then we want to think of adding consecutive integers 1 + 2 + 3 + … until the sum reaches or exceeds the limit. The program should print out how many terms were in the sum. For example, if the limit is 7, 1+2+3 gives 6, and 1+2+3+4 gives 10; so we should print 4.
While-Statement example limit = input (“Enter limit for sum”) total = 0 num = 1 while total < limit : total = total + num num = num + 1 print num Do example Discount now.
While-Statement example Do example Discount now.
While-Statement example Do example Discount now.
While-Statement example Do example Discount now.
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 = 1 while (counter <= 10) : // Executes 10 passes <do something> counter = counter + 1 counter = 1 while (counter < 10) : // Executes 9 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.
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
GCD The largest integer that evenly divides into two non-negative integers Example: gcd(32, 24) = 8 Algorithm discovered by Euclid was one of the first algorithms ever discovered
Flash from the Past Euclid’s Algorithm for Greatest Common Divisor Get two positive integers as input, call them I and J. Divide I by J, and call the Remainder R. If R is not 0, then reset I to the value of J, reset J to the value of R, and go back to step 2. Print out the answer, which is the value of J. Stop
Example GCD(206, 40)
Example GCD(206, 40) = GCD(40, 6)
Example GCD(206, 40) = GCD(40, 6) = GCD(6, 4)
Example GCD(206, 40) = GCD(40, 6) = GCD(6, 4) = GCD(4, 2)
Example GCD(206, 40) = GCD(40, 6) = GCD(6, 4) = GCD(4, 2) = GCD(2, 0)
Example GCD(206, 40) = GCD(40, 6) = GCD(6, 4) = GCD(4, 2) = GCD(2, 0) = 2
Pseudocode Get I, J Set R to remainder of I divided by J While R≠0 Do Set I to J Set J to R Set R to remainder of I divided by J end-of-loop Print J
Code
Some comments on programming style Use meaningful variable names. Use comments for your name, purpose of program, date. Include a pseudocode algorithm in comments, if appropriate. Use comments to mark off sections of code and to clarify “tricky” code.
Keep lookin, boys, we'll findem somewhere