Break and continue in MATLAB - Time & Space Complexity
When we use break and continue in loops, it changes how many times the loop runs.
We want to see how these commands affect the total work done as the input grows.
Analyze the time complexity of the following code snippet.
for i = 1:n
if i == 5
break;
end
disp(i);
end
This code prints numbers from 1 up to 4, then stops the loop early using break.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that runs from 1 to n.
- How many times: Normally n times, but break stops it early at i == 5.
Because break stops the loop at 5, the number of operations stays the same no matter how big n gets.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 4 |
| 100 | 4 |
| 1000 | 4 |
Pattern observation: The loop stops early, so work does not grow with input size.
Time Complexity: O(1)
This means the loop runs a fixed number of times, no matter how big the input is.
[X] Wrong: "The loop always runs n times even with break."
[OK] Correct: Break stops the loop early, so it can run fewer times than n.
Understanding how break and continue affect loops helps you explain code efficiency clearly and confidently.
"What if we replaced break with continue? How would the time complexity change?"
Practice
break statement do inside a MATLAB loop?Solution
Step 1: Understand the purpose of
Thebreakbreakstatement is used to exit a loop immediately, no matter where it is in the loop.Step 2: Compare with other loop controls
Unlikecontinue, which skips only the current iteration,breakstops the whole loop.Final Answer:
Stops the entire loop immediately and exits it. -> Option AQuick Check:
break = stop loop [OK]
- Confusing break with continue
- Thinking break only skips one iteration
- Believing break restarts the loop
for loop?Solution
Step 1: Identify the statement to skip iteration
In MATLAB,continueis the keyword used to skip the rest of the current loop iteration and move to the next.Step 2: Check other options
breakexits the loop,skipandnextare not valid MATLAB keywords.Final Answer:
continue; -> Option AQuick Check:
continue = skip iteration [OK]
- Using break instead of continue
- Writing skip or next which are invalid
- Omitting the semicolon (though optional)
sum = 0;
for i = 1:5
if i == 3
break;
end
sum = sum + i;
end
sumSolution
Step 1: Trace the loop iterations
The loop runs i = 1, 2, then at i = 3 the break triggers and loop stops.Step 2: Calculate sum before break
sum = 0 + 1 + 2 = 3; The addition at i=3 does not happen because break stops loop before sum update.Final Answer:
3 -> Option BQuick Check:
Sum before break = 3 [OK]
- Adding i=3 before break
- Summing all numbers ignoring break
- Confusing break with continue
for i = 1:5
if i == 3
continue
end
disp(i)
endSolution
Step 1: Check syntax of continue
In MATLAB,continuedoes not require a semicolon and is valid inside loops.Step 2: Understand loop behavior
When i == 3,continueskipsdisp(i), so 3 is not displayed. This is expected behavior.Final Answer:
No error, code runs correctly -> Option CQuick Check:
continue valid in loops [OK]
- Thinking semicolon is mandatory after continue
- Believing continue is invalid in for loops
- Expecting disp to print 3
break and continue?Solution
Step 1: Understand conditions order
We must skip adding 5 (continue when i==5) and stop loop if sum > 30 (break after adding).Step 2: Analyze each option
sum = 0; for i = 1:10 if i == 5 continue; end sum = sum + i; if sum > 30 break; end end skips 5 before adding, then adds i, then checks sum > 30 to break. This matches requirements.Final Answer:
C -> Option DQuick Check:
Skip 5 with continue; break after sum check [OK]
- Breaking when i==5 instead of continuing
- Checking sum > 30 before adding
- Using continue to skip after sum check
Ready to prove it?
Pass the test and this pattern moves unknown
