200 likes | 383 Views
Computer Science 101. The while Loop. Increment and Decrement. Increase or decrease the value of a variable by 1. number = 10 number = number + 1 number = number - 1. Example Problem. Print all the numbers from 1 through 5. number = 1 print number number = number + 1 print number
E N D
Computer Science 101 The while Loop
Increment and Decrement • Increase or decrease the value of a variable by 1 number = 10 number = number + 1 number = number - 1
Example Problem • Print all the numbers from 1 through 5 number = 1 print number number = number + 1 print number number = number + 1 print number number = number + 1 print number number = number + 1 print number
Example Problem • Print all the numbers from 1 through 100 number = 1 print number number = number + 1 print number number = number + 1 print number number = number + 1 print number number = number + 1 print number # Yikes!!!!!
Generalize with a Loop • Print all the numbers from 1 through 100 number = 1 limit = 100 while number <= limit: print number number = number + 1 Can be used with any limit, no matter how large Amount of code stays fixed as problem size grows
Generalize with a Loop • Print all the numbers from 1 through an input limit number = 1 limit = input("Enter the limit: ") while number <= limit: print number number = number + 1 This type of loop is also called a count-controlled loop
Syntax of the while Loop while <Boolean expression>: <statement-1> <statement-2> … <statement-n> loop header loop body A Boolean expression can be a comparison of two numbers or strings, using ==, !=, <, >, <=, >= These operations return the Boolean values True or False Note that == means equal to, whereas = means assignment
Behavior of the while Loop Boolean expression False True Loop body while <Boolean expression>: # The condition <sequence of statements> # The loop body
Logic of the while Loop loop control variable number = 1 limit = input("Enter the limit: ") while number <= limit: print number number = number + 1 Before the loop header, the loop control variable must be set to the appropriate initial value Somewhere in the loop body, a statement must reset the loop control variable so that the Boolean expression eventually becomes False, to terminate the loop
Another Example: Sums • Print the sum of the numbers from 1 through an input limit number = 1 limit = input("Enter the limit: ") sum = 0 while number <= limit: sum = sum + number number = number + 1 print "The sum is", sum The variable sum accumulates a value during the execution of the loop
Tracing the Variables Understand behavior by printing the values of the variables number = 1 limit = input("Enter the limit: ") sum = 0 while number <= limit: sum = sum + number print number, sum number = number + 1 print "The sum is", sum Enter the limit: 3 1 1 2 3 3 6 The sum is 6
Extended Assignment The increment of a variable, such as x = x + 1 is so common that Python includes some shorthand for it x += 1
Any Arithmetic Operation x = x + 1 x += 1 x = x - 1 x -= 1 x = x * 2 x *= 2 x = x / 2 x /= 2 number = 1 limit = input("Enter the limit: ") sum = 0 while number <= limit: sum += number number += 1 print "The sum is", sum
Sentinel-Controlled Loops • A loop can test for the presence of a special value called a sentinel • When the sentinel is reached, the loop should stop
An Example Problem • Input test scores from the user and print the average score. • The user signals the end of input by simply pressing the enter or return key. • This value shows up as an empty string, which is the sentinel value.
Numeric vs Text Input number = input("Enter a number: ") # Do something with the number The input function expects a number from the keyboard Any data that do not look like numbers, such as a simple return, will raise an error
Numeric vs Text Input number = input("Enter a number: ") # Do something with the number The input function expects a number from the keyboard number = raw_input("Enter a number or return to quit: ") if number != "": number = int(number) # Do something with number The raw_input function accepts any text from the keyboard
Sentinel-Controlled Loops sum = 0 count = 0 data = raw_input("Enter a score or just return to quit ") while data != "": sum += int(data) count += 1 data = raw_input("Enter a score or just return to quit ") if count == 0: print "No scores were entered" else: print "The average is", sum / float(count) The input statement must be written twice
Simplify with a break Statement sum = 0 count = 0 while True: data = raw_input("Enter a score or just return to quit ") if data == "": break sum += int(data) count += 1 if count == 0: print "No scores were entered" else: print "The average is", sum / count break causes an immediate exit from the enclosing loop
When Should I Use a break? • When the loop body executes at least once but the number of iterations is unpredictable • The condition of the enclosing while loop is just True • The condition for loop exit is placed in an enclosing if statement