Break statement in Java - Time & Space Complexity
Let's see how using a break statement affects how long a loop runs.
We want to know how the number of steps changes when the input grows.
Analyze the time complexity of the following code snippet.
for (int i = 0; i < n; i++) {
if (i == 5) {
break;
}
System.out.println(i);
}
This code prints numbers from 0 up to 4 and stops early because of the break.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that runs from 0 to n-1.
- How many times: Normally n times, but here it stops after 5 times because of break.
Even if n grows bigger, the loop stops early at 5 steps.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5 |
| 100 | 5 |
| 1000 | 5 |
Pattern observation: The number of steps stays the same no matter how big n gets.
Time Complexity: O(1)
This means the loop runs a fixed number of times, not growing with input size.
[X] Wrong: "The loop always runs n times even with break."
[OK] Correct: The break stops the loop early, so it may run fewer times than n.
Understanding how break changes loop steps helps you explain code efficiency clearly.
"What if the break condition was i == n/2? How would the time complexity change?"
Practice
break statement do inside a loop in Java?Solution
Step 1: Understand the purpose of break
The break statement is used to exit the nearest loop or switch immediately when executed.Step 2: Compare with other loop controls
Unlike continue which skips to the next iteration, break stops the loop entirely.Final Answer:
It immediately stops the loop and exits it. -> Option CQuick Check:
Break stops loop [OK]
- Confusing break with continue
- Thinking break pauses the loop
- Using break outside loops or switch
break inside a Java for loop?Solution
Step 1: Check correct placement of break
Break must be inside a block and triggered by a condition likeif(i==3).Step 2: Validate Java syntax
for(int i=0; i<5; i++) { if(i==3) break; } uses correct Java syntax:if(condition) break;inside the loop block.Final Answer:
for(int i=0; i<5; i++) { if(i==3) break; } -> Option BQuick Check:
Correct break syntax [OK]
- Placing break outside braces
- Using break as a function call
- Incorrect if-break order
for(int i=1; i<=5; i++) {
if(i == 3) {
break;
}
System.out.print(i + " ");
}Solution
Step 1: Trace loop iterations
Loop starts i=1, prints 1; i=2, prints 2; at i=3, break triggers and loop stops.Step 2: Determine printed output
Only 1 and 2 are printed before break stops the loop.Final Answer:
1 2 -> Option AQuick Check:
Break at i=3 stops loop, output = 1 2 [OK]
- Assuming break prints 3
- Ignoring break and printing all numbers
- Confusing break with continue
int i = 0;
while(i < 5)
if(i == 3)
break;
i++;
System.out.println(i);Solution
Step 1: Analyze loop structure and indentation
Without braces, only the first statement after while is inside the loop; i++ runs outside the loop.Step 2: Understand effect on loop
Because i++ is outside, i never increments inside loop, causing infinite loop or wrong behavior.Final Answer:
Missing braces causing incorrect loop behavior -> Option AQuick Check:
Braces needed to group loop statements [OK]
- Assuming break is invalid in while
- Ignoring indentation and braces
- Thinking i++ runs inside loop
break to achieve this?int[] nums = {10, 13, 21, 28, 35};
int result = -1;
for(int n : nums) {
// Fill here
}
System.out.println(result);Solution
Step 1: Understand the goal
We want to assign the first divisible by 7 number to result and then stop the loop immediately.Step 2: Check correct break usage
if(n % 7 == 0) { result = n; break; } assigns result first, then breaks to stop searching. This is correct.Step 3: Analyze incorrect options
if(n % 7 == 0) { break; result = n; } breaks before assigning result, so result stays -1. Options A and D use continue which skips to the next iteration and loop continues.Final Answer:
if(n % 7 == 0) { result = n; break; } -> Option DQuick Check:
Assign then break to stop loop [OK]
- Breaking before assignment
- Using continue instead of break
- Not stopping loop after finding value
Ready to prove it?
Pass the test and this pattern moves unknown
