What Is a While Loop?
You previously learned about if statements, something you use if you’re testing a boolean value one time. Once this test is done, you cannot check the condition again.
When Would You Use a While Loop?
- While loops are useful when you need something to be repeated until a certain condition is met
- For example: playing a 20 questions game that needs a question to be repeated until the correct answer is given
- If you find yourself repeating the same if loop over and over again to accomplish a task, then you should probably use a while loop instead
Let’s see how that would look if a while loop was used instead.