Loops

What is a loop?

A loop is a block of code that runs many different times.

What are loops used for?

Loops are used when you need to repeat actions more than once, depending on specific criteria. The two loop types are the for loop and the while loop.

What is a for loop?

A for loop repeats as many as you specify or once for each item in a sequence. For example, you can write a for loop that runs a hundred tmes to repeat an action a hundred times. You can also write a for loop that runs once for each item in a collection of items.

What is a while loop?

A while loop runs as long as a specific condition is true. Say you have a gam loop that tuns as long as a game_activate variable is set to True. You can then put multiple situations inside the loop that cause game_active to become False and to end the game. You can also use a while loop to process items in a collection that might have new items added to it as the loop is running. As long as items are in the collection, you can continue working with it. When the collection is empty, the loop quits running.

What is a nested loop?

A nested loop is a loop inside another loop. Say you need a loop that examines all of an image's pixels. You could write a loop that looks at each pixel in a row and, inside that loop, add a second loop that looks at every pixel in that row's current column.