Iteration Recipes
Recipes for iterating over, looping through, and repeating stuff:
-
Repeat Code a Specific Number of Times: I want to repeatedly execute a chunk of code, counting the repetitions.
-
Repeat While or Until Something Is True: I want to repeatedly execute a chunk of code while or until some condition is true, however many times that may be.
-
Stop Iteration Early: Sometimes my code needs to stop iterating before a loop has reached its normal end (of the range in
for
, boolean termination condition inwhile
). -
Flip a Coin or Roll a Die: My code needs to randomly pick or decide something.
-
Determine Statistics of a Sequence: I want to determine some simple statistical measures (like frequency, mean, and standard deviation) of a set of measurements, events, or data, and I don't want to store all of the individual values in memory.