Lesson 11
Loops
Loops allow you to execute a block of code multiple times until a certain condition is met.
- for Loop
- while Loop
- do...while Loop
- break & continue
- Nested Loops
forfor Loop
Use when you know how many times to iterate.
for (let i = 0; i < n; i++) { // code}Best for: Count based loops
whilewhile Loop
Use when the number of iterations is unknown.
while (condition) { // code}Best for: Condition based loops
dodo...while Loop
Similar to while, but executes at least once.
do { // code} while (condition);Best for: Menu, validations
JavaScript
Output
Your output will appear here...
for (init; condition; update) { // code}while (condition) { // code}do { // code} while (condition);
Always ensure your loop condition will eventually become false, otherwise you may create an infinite loop!
Use a for loop when you know the count, a while loop when you don’t, and do...while when the code must run at least once. break exits the loop early and continue skips to the next iteration.
