Loops

This describes how to loop around and run many times.

Learn about Steps, Storage, Values, and Flow before learning this page.

While

Imagine you are riding a merry-go-round and there is a pile of rocks by the side. Each time you go around, you want to pick up one rock and put it into a box. Another way to thing about it is steps along a path. Each indented line is a step along the path, and each step means that the line has been run.

The "while True" means "keep run the following indented lines".
The 'break' is a special word that means "skip the indented lines and quit running the loop"

The steps every time through the loop look like this.

Another While

The more common way to do the loop is shown here. Now the "while True:" has been replaced to check what the contents of boxA is.

For

Python has some special ways to do "for" loops. Probably the most common way is using range with a start value, and another value that it stops before.

There is also a way to get a value from a list each time through the loop.