Labeled break and continue in Javascript - Time & Space Complexity
We want to see how using labeled break and continue affects how long a program takes to run.
Specifically, we ask: how does skipping or stopping loops early change the work done as input grows?
Analyze the time complexity of the following code snippet.
outerLoop: for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (j === i) continue outerLoop;
if (i + j > n) break outerLoop;
}
}
This code uses labeled break and continue to jump out of or skip parts of loops based on conditions.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops over i and j from 0 to n.
- How many times: Up to n times for outer loop, and up to n times for inner loop, but breaks and continues can stop inner loop early.
As n grows, the loops try to run more times, but labeled break and continue can stop inner loops early.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Less than 100 due to early breaks |
| 100 | Less than 10,000 but more than 1,000 |
| 1000 | Less than 1,000,000 but more than 100,000 |
Pattern observation: The work grows roughly with the square of n, but early exits reduce some work.
Time Complexity: O(n²)
This means the time grows roughly like the square of the input size, even with labeled breaks and continues.
[X] Wrong: "Using labeled break or continue always makes the code run in constant time."
[OK] Correct: Even though these statements can skip some work, the loops still mostly run many times as input grows, so time still grows with input size.
Understanding how labeled break and continue affect loop execution helps you explain code efficiency clearly and confidently in interviews.
"What if we removed the labeled break and continue and used only regular break and continue inside the inner loop? How would the time complexity change?"
Practice
What does the break label; statement do in JavaScript?
Solution
Step 1: Understand labeled break
Thebreak label;statement exits the loop that has the specified label, not just the innermost loop.Step 2: Compare with normal break
Normalbreakexits only the current loop, but labeled break exits the labeled outer loop immediately.Final Answer:
It exits the loop identified by the label immediately. -> Option AQuick Check:
break label exits labeled loop [OK]
- Thinking break label only skips current iteration
- Confusing break label with continue label
- Assuming break label pauses the loop
Which of the following is the correct syntax to continue the outer loop labeled outerLoop?
outerLoop: for(let i = 0; i < 3; i++) {
for(let j = 0; j < 3; j++) {
// What goes here?
}
}Solution
Step 1: Recall labeled continue syntax
The correct syntax to continue a labeled loop iscontinue labelName;without parentheses or extra words.Step 2: Match options with syntax
continue outerLoop; matches the correct syntax exactly. Options B, C, and D are invalid syntax.Final Answer:
continue outerLoop; -> Option AQuick Check:
continue labelName; is correct syntax [OK]
- Adding parentheses around label
- Using colon after continue
- Writing extra words like 'to'
What is the output of the following code?
outer: for(let i = 1; i <= 3; i++) {
for(let j = 1; j <= 3; j++) {
if(j === 2) continue outer;
console.log(i, j);
}
}Solution
Step 1: Understand continue outer;
Whenj === 2,continue outer;skips to the next iteration of the outer loop, skipping the rest of inner loop and console.log for that i.Step 2: Trace iterations
For i=1: j=1 logs '1 1', j=2 triggers continue outer (skip to i=2). For i=2: j=1 logs '2 1', j=2 triggers continue outer (skip to i=3). For i=3: j=1 logs '3 1', j=2 triggers continue outer (loop ends after i=3).Final Answer:
1 1 2 1 3 1 -> Option BQuick Check:
continue outer skips inner loop rest [OK]
- Thinking continue outer skips only inner iteration
- Expecting all inner loop logs
- Confusing break and continue effects
Find the error in this code snippet:
outerLoop: for(let i = 0; i < 2; i++) {
for(let j = 0; j < 2; j++) {
if(i === j) break;
if(j === 1) continue outerLoop;
console.log(i, j);
}
}Solution
Step 1: Analyze break statement
Thebreak;without label breaks only the innermost loop, not the labeled outer loop.Step 2: Check labeled continue usage
Thecontinue outerLoop;is valid and skips to next iteration of the labeled loop.Final Answer:
Thebreak;statement breaks only inner loop, not labeled loop. -> Option CQuick Check:
break without label affects inner loop only [OK]
- Assuming break breaks labeled loop without label
- Thinking continue outerLoop is invalid
- Confusing label syntax
Given the nested loops below, how can you skip all remaining iterations of the outer loop when j === 3?
outerLoop: for(let i = 1; i <= 5; i++) {
for(let j = 1; j <= 5; j++) {
if(j === 3) {
// What statement here?
}
console.log(i, j);
}
}Solution
Step 1: Understand the goal
We want to stop all remaining iterations of the outer loop whenj === 3, meaning exit the outer loop completely.Step 2: Choose correct statement
break outerLoop;exits the labeled outer loop immediately. Plainbreak;exits only inner loop.continue;andcontinue outerLoop;skip iterations but do not exit the outer loop.Final Answer:
break outerLoop; -> Option DQuick Check:
break label exits labeled loop [OK]
- Using break without label to exit outer loop
- Using continue instead of break
- Confusing continue label with break label
Ready to prove it?
Pass the test and this pattern moves unknown
