For Loop vs While Loop
A for loop goes over a list and stops when the list runs out. A while loop goes on until its test turns false.
For Loop
A loop that runs once for each item in a list, then stops by itself.
A for loop runs its lines once for each item in a list or a range. Each time round, a variable holds the next item. When the items run out, the loop ends by itself. Python writes it as for name in names. Other languages, like C and JavaScript, often count with a number instead.
While Loop
A loop that keeps going while a test is true, and stops when it turns false.
A while loop keeps running as long as a test stays true. It checks the test before every pass. When the test turns false, the loop stops. You use it when you do not know ahead of time how many passes you need. Something inside the loop must change the answer, or it never ends.
In one line
- For Loop
- A loop that runs once for each item in a list, then stops by itself.
- While Loop
- A loop that keeps going while a test is true, and stops when it turns false.