Why loop control is required in Javascript - Performance Analysis
When we use loops in code, the time it takes to run depends on how many times the loop runs.
We want to understand why controlling loops is important to keep the program efficient.
Analyze the time complexity of the following code snippet.
let n = 5;
let i = 0;
while (i < n) {
console.log(i);
i++;
}
This code prints numbers from 0 to 4 by increasing i each time until it reaches 5.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop runs repeatedly.
- How many times: It runs 5 times, once for each number from 0 to 4.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 5 | 5 |
| 10 | 10 |
| 100 | 100 |
Pattern observation: The number of operations grows directly with the input size. More input means more loop runs.
Time Complexity: O(n)
This means the time to run the loop grows in a straight line as the input size increases.
[X] Wrong: "The loop will always run fast no matter what."
[OK] Correct: Without proper control, loops can run too many times or even forever, making the program slow or stuck.
Understanding how loops grow with input helps you write code that runs efficiently and avoids problems like infinite loops.
"What if we forgot to increase i inside the loop? How would the time complexity change?"
Practice
break and continue in JavaScript loops?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]
- Thinking loops always run fully without control
- Confusing variable creation with loop control
- Believing loop control prints output
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]
- Using 'stop' or 'exit' which are invalid in JavaScript
- Confusing 'continue' with 'break'
- Missing semicolon after 'break'
for(let i = 1; i <= 5; i++) {
if(i === 3) continue;
console.log(i);
}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]
- Including 3 in output despite continue
- Thinking continue stops the loop
- Confusing continue with break
for(let i = 0; i < 5; i++) {
if(i === 2) break
console.log(i);
}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]
- Ignoring missing semicolon errors
- Thinking break cannot be used in loops
- Confusing var and let declarations
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]
- Swapping break and continue roles
- Not using any loop control causing wrong output
- Trying to stop loop with continue
Ready to prove it?
Pass the test and this pattern moves unknown
