for vs while Choice
Question
When should you prefer a for loop over a while loop?
Hint
Think about what information you have before the loop starts.
Explanation
Use a for loop when you know how many times to iterate (e.g., looping over an array of known length). Use a while loop when the number of iterations is unknown and depends on a condition that changes during execution (e.g., reading user input until they type 'quit'). Neither is universally better.
Related Resources
Related Lessons
- Loops (for & while)
Learn how for and while loops work.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.