Understanding Control Flow Loops: While Loops in CS112 at GMU
Explore the concept of control flow and while loops in CS112 at GMU through a series of informative images covering different scenarios and tests. Learn about possible code paths, decision-making structures, and the characteristics of while loops. Dive into flow charts, understanding loops, and the essence of while loops as you navigate through the fundamentals of programming.
Download Presentation
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
Control Flow: Loops While Loops CS 112 @ GMU
What tests do we need? #1 True False c if c : s1 s1 s2 s2
What tests do we need? #2 True if c1 : s1 elif c2: s2 s3 False c1 True False s1 c2 s2 s3
What tests do we need? #3 True if c1 : s1 elif c2: s2 else: s3 s4 False c1 True False s1 c2 s2 s3 s4
What tests do we need? #4 s0 s0 if c1: if c2: if c3: s4 what possible paths are there through this code? False True c1 s1 s1 would this code behave differently if we used elif's for c2 and c3? False True c2 s2 s2 False True c3 s3 s3 s4
What tests do we need? #5 if c1: else: s9 False True c1 s1 if c2: s3 s1 s4 s2 True False True False c2 c3 s2 s4 if c3: else: s8 s5 s6 True False s5 c4 s3 s7 s6 if c4: s7 s8 s9
Flow Charts Loops cycle? then loop! s1 c True False actions in the cycle? "body" of the loop s2 s3 repeatedly make the same decision need to be able to exit it! Loops CS112 Fall 2016 GMU 10
While Loop keep running the same block of code if boolean expression is still True could keep running forever stop if boolean expression is False expression should be able to change!
the while loop Expression that evaluates to True or False while bool_expr: body_stmts As many body statements as you want, but make sure they are indented properly! Body statements could be more structures, like an if or another while!
the while loop True expression statement False
Flow Charts #1 s1 s1 while c: s2 s3 False True c s2 s3
Flow Charts #2 s1 while c1: s2 if c2: s3 s4 s1 False True c1 s2 True False c2 s4 s3
Flow Charts #3 s1 if c1: s2 else: s3 while c2: s4 s5 s6 s1 True False c1 s3 s2 True False c2 s5 s4 s6
While Loop Example #1 guess = input("password: ") while guess != "Mason": guess = input("wrong, try again: ") print("welcome!") every time the user types wrong password, we will run the loop's body another time
While Loop Example #2 last = int(input("last value: ")) count = 1 while count <= last: print(count) count += 1 print("done") prints numbers from 1 to last, then prints done. what happens when last is not positive?
Practice Problem Write a program that: Allows the user to enter a number Verifies that number is positive (> 0) Continuously prompts the user until they enter a positive number
Practice Problem Write a program that: Allows the user to enter a positive number Prints every number from one until that number Unless that number is even, then print "even" instead 1 even 3 even
Practice Problem Write a program that: allows the user to guess a password If they fail to guess the password 3 times, stop allowing them to guess
More Options break: immediately exit the loop don't finish current iteration; skip remaining iterations nested loops? only exits the closest (inner-most) continue: immediately begin next iteration don't finish current iteration; "continue" with next iteration nested loops? only slips inner most loop iteration: "one execution of the indented body of statements"
break Example password = "swordfish" guess = "" num_tries = 0 while guess != password: if num_tries >= 3: print("too many guesses!") break guess = input ("what's the password? ") num_tries += 1 print("done.") # anything except swordfish will work here Sample Run what's the password? paper what's the password? rock what's the password? scissors too many guesses! done. Note: breaks/continues almost always hide inside an if-stmt.
continue Example n = int ( input ( "stopval: ")) print ("skipping 3- 5-multiples") Sample Run i = 0 while i < n: i += 1 if i%3==0: continue if i%5==0: continue print(i) stopval: 16 skipping 3- 5-multiples 1 2 4 7 8 11 13 14 16
Break & Continue Usage Note: included in most programming languages, but easy to abuse/misuse Harder to read, debug, or make flow charts for Use them mindfully only when they make your code exceptionally clearer to understand or exceptionally more efficient. 27
Game With Loops! Remember the guessing game? We can write it better now!