We return to the topic of iteration! A
while loop allows us to deal with indefinite iteration--iterating an unknown (or not easily knowable) number of times until some stopping condition is reached.
breakin any work you submit in CS1110. We want you to know about this keyword, but you should not use it (for now). Instead, practice using the
whileloop in situations where the
whileloop is well suited.
forloop and the
whileloop for calculating the sums of squares (total.py, flow.py)
whileloops: use a goal condition; use a boolean tracking variable (pattern2.py, pattern3.py)
Optional extra practice (last slide): Modify the
game module from last lecture for guessing a secret word to use a
while loop instead of recursion. Play the game to check your code! Then take a look at our solution here. Which solution do you prefer, recursion or
Optional reading: On loop invariants