1 / 20

Computer Science 101

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

Download Presentation

Computer Science 101

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 101 The while Loop

  2. Increment and Decrement • Increase or decrease the value of a variable by 1 number = 10 number = number + 1 number = number - 1

  3. 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

  4. 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!!!!!

  5. 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

  6. 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

  7. 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

  8. Behavior of the while Loop Boolean expression False True Loop body while <Boolean expression>: # The condition <sequence of statements> # The loop body

  9. 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

  10. 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

  11. 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

  12. Extended Assignment The increment of a variable, such as x = x + 1 is so common that Python includes some shorthand for it x += 1

  13. 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

  14. 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

  15. 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.

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

More Related