Loop control helps us manage how many times a loop runs. It stops loops from running forever and lets us skip or end loops early.
Why loop control is required in Javascript
Introduction
Syntax
Javascript
for (let i = 0; i < 10; i++) { // loop body if (condition) { break; // stops the loop } if (otherCondition) { continue; // skips to next loop cycle } }
break stops the whole loop immediately.
continue skips the rest of the current loop cycle and moves to the next one.
Examples
Javascript
for (let i = 0; i < 5; i++) { if (i === 3) { break; } console.log(i); }
Javascript
for (let i = 0; i < 5; i++) { if (i === 2) { continue; } console.log(i); }
Sample Program
This program prints odd numbers from 1 to 5. It skips even numbers and stops the loop when i reaches 6.
Javascript
for (let i = 1; i <= 10; i++) { if (i === 6) { break; } if (i % 2 === 0) { continue; } console.log(i); }
Important Notes
Without loop control, loops might run forever and crash your program.
Use break to stop loops early when you find what you need.
Use continue to skip steps you don't want to run inside the loop.
Summary
Loop control helps manage how loops run and stop.
break stops the loop completely.
continue skips to the next loop cycle.
Practice
1. Why do we need loop control statements like
break and continue in JavaScript loops?easy
Solution
Step 1: Understand loop control purpose
Loop control statements help manage the flow inside loops, deciding when to stop or skip parts.Step 2: Identify correct use
breakstops the loop early,continueskips to next iteration, both control loop behavior.Final Answer:
To control when loops stop or skip steps -> Option AQuick Check:
Loop control = manage loop flow [OK]
Hint: Loop control changes loop flow by stopping or skipping [OK]
Common Mistakes:
- Thinking loops always run fully without control
- Confusing variable creation with loop control
- Believing loop control prints output
2. Which of the following is the correct syntax to stop a loop immediately in JavaScript?
easy
Solution
Step 1: Recall loop control keywords
JavaScript usesbreakto stop loops immediately.Step 2: Check other options
stopandexitare not valid JavaScript keywords;continueskips to next iteration but does not stop loop.Final Answer:
break; -> Option AQuick Check:
Stop loop = break [OK]
Hint: Use 'break' to stop loops immediately [OK]
Common Mistakes:
- Using 'stop' or 'exit' which are invalid in JavaScript
- Confusing 'continue' with 'break'
- Missing semicolon after 'break'
3. What will be the output of this code?
for(let i = 1; i <= 5; i++) {
if(i === 3) continue;
console.log(i);
}medium
Solution
Step 1: Understand the loop and condition
The loop runs from 1 to 5. Wheniis 3,continueskips the rest of that iteration.Step 2: Trace output values
Numbers 1 and 2 print normally. At 3, loop skips printing. Then 4 and 5 print.Final Answer:
1 2 4 5 -> Option CQuick Check:
Continue skips 3, so output excludes 3 [OK]
Hint: Continue skips current loop step, so that value is not printed [OK]
Common Mistakes:
- Including 3 in output despite continue
- Thinking continue stops the loop
- Confusing continue with break
4. Find the error in this loop code:
for(let i = 0; i < 5; i++) {
if(i === 2) break
console.log(i);
}medium
Solution
Step 1: Check syntax of break statement
JavaScript requires semicolon after statements; missing semicolon afterbreakcauses syntax error.Step 2: Validate other parts
Loop condition and variable declaration are correct; break is allowed inside loops.Final Answer:
Missing semicolon after break statement -> Option DQuick Check:
Semicolon needed after break [OK]
Hint: Always put semicolon after break to avoid syntax errors [OK]
Common Mistakes:
- Ignoring missing semicolon errors
- Thinking break cannot be used in loops
- Confusing var and let declarations
5. You want to print numbers from 1 to 10 but skip multiples of 3 and stop the loop if number reaches 8. Which loop control statements should you use?
hard
Solution
Step 1: Identify skipping condition
To skip multiples of 3, usecontinueto jump to next iteration without printing.Step 2: Identify stopping condition
To stop loop when number reaches 8, usebreakto exit loop immediately.Final Answer:
Usecontinueto skip multiples of 3 andbreakto stop at 8 -> Option BQuick Check:
Skip = continue, stop = break [OK]
Hint: Continue skips steps; break stops loop completely [OK]
Common Mistakes:
- Swapping break and continue roles
- Not using any loop control causing wrong output
- Trying to stop loop with continue
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
