Bird
Raised Fist0
MATLABdata~20 mins

Break and continue in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Loop Control Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of break in a for loop
What is the output of this MATLAB code?
for i = 1:5
    if i == 3
        break;
    end
    disp(i)
end
MATLAB
for i = 1:5
    if i == 3
        break;
    end
    disp(i)
end
A
1
2
3
B
1
2
C
1
2
3
4
5
D
3
4
5
Attempts:
2 left
💡 Hint
The break statement stops the loop immediately when the condition is met.
❓ Predict Output
intermediate
2:00remaining
Output of continue in a for loop
What is the output of this MATLAB code?
for i = 1:5
    if i == 3
        continue;
    end
    disp(i)
end
MATLAB
for i = 1:5
    if i == 3
        continue;
    end
    disp(i)
end
A
1
2
4
5
B
1
2
3
4
C3
D
1
2
3
4
5
Attempts:
2 left
💡 Hint
The continue statement skips the current iteration when the condition is met.
🔧 Debug
advanced
2:00remaining
Identify the error in break usage
What error does this MATLAB code produce?
if true
    break;
end
MATLAB
if true
    break;
end
ALogical error: infinite loop
BRuntime error: variable 'break' undefined
CSyntax error: 'break' not inside a loop
DNo error, code runs fine
Attempts:
2 left
💡 Hint
Break can only be used inside loops in MATLAB.
❓ Predict Output
advanced
2:00remaining
Nested loops with break and continue
What is the output of this MATLAB code?
for i = 1:3
    for j = 1:3
        if j == 2
            break;
        end
        fprintf('[%%d %%d]\n',i,j)
    end
end
MATLAB
for i = 1:3
    for j = 1:3
        if j == 2
            break;
        end
        fprintf('[%d %d]\n',i,j)
    end
end
A
[1 1]
[1 2]
[2 1]
[3 1]
B
[1 1]
[1 2]
[2 1]
[2 2]
[3 1]
[3 2]
C
[1 1]
[1 2]
[1 3]
[2 1]
[2 2]
[2 3]
[3 1]
[3 2]
[3 3]
D
[1 1]
[2 1]
[3 1]
Attempts:
2 left
💡 Hint
The break stops the inner loop when j equals 2, so only j=1 is displayed for each i.
❓ Predict Output
expert
3:00remaining
Complex loop control with break and continue
What is the output of this MATLAB code?
count = 0;
for i = 1:5
    if mod(i,2) == 0
        continue;
    end
    count = count + 1;
    if count == 3
        break;
    end
    disp(i)
end
MATLAB
count = 0;
for i = 1:5
    if mod(i,2) == 0
        continue;
    end
    count = count + 1;
    if count == 3
        break;
    end
    disp(i)
end
A
1
3
B
1
3
5
C
1
3
5
7
D
2
4
Attempts:
2 left
💡 Hint
Count only increases for odd numbers. When count reaches 3, break executes before disp(i).

Practice

(1/5)
1. What does the break statement do inside a MATLAB loop?
easy
A. Stops the entire loop immediately and exits it.
B. Skips the current iteration and continues with the next one.
C. Restarts the loop from the beginning.
D. Pauses the loop until a condition is met.

Solution

  1. Step 1: Understand the purpose of break

    The break statement is used to exit a loop immediately, no matter where it is in the loop.
  2. Step 2: Compare with other loop controls

    Unlike continue, which skips only the current iteration, break stops the whole loop.
  3. Final Answer:

    Stops the entire loop immediately and exits it. -> Option A
  4. Quick Check:

    break = stop loop [OK]
Hint: Break stops loop fully; continue skips one step [OK]
Common Mistakes:
  • Confusing break with continue
  • Thinking break only skips one iteration
  • Believing break restarts the loop
2. Which of the following is the correct syntax to skip the current iteration in a MATLAB for loop?
easy
A. continue;
B. next;
C. break;
D. skip;

Solution

  1. Step 1: Identify the statement to skip iteration

    In MATLAB, continue is the keyword used to skip the rest of the current loop iteration and move to the next.
  2. Step 2: Check other options

    break exits the loop, skip and next are not valid MATLAB keywords.
  3. Final Answer:

    continue; -> Option A
  4. Quick Check:

    continue = skip iteration [OK]
Hint: Use continue to skip current loop step [OK]
Common Mistakes:
  • Using break instead of continue
  • Writing skip or next which are invalid
  • Omitting the semicolon (though optional)
3. What will be the output of this MATLAB code?
sum = 0;
for i = 1:5
    if i == 3
        break;
    end
    sum = sum + i;
end
sum
medium
A. 10
B. 3
C. 6
D. 15

Solution

  1. Step 1: Trace the loop iterations

    The loop runs i = 1, 2, then at i = 3 the break triggers and loop stops.
  2. 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.
  3. Final Answer:

    3 -> Option B
  4. Quick Check:

    Sum before break = 3 [OK]
Hint: Break stops loop before adding i=3 [OK]
Common Mistakes:
  • Adding i=3 before break
  • Summing all numbers ignoring break
  • Confusing break with continue
4. Find the error in this MATLAB code snippet:
for i = 1:5
    if i == 3
        continue
    end
    disp(i)
end
medium
A. Missing semicolon after continue
B. disp(i) will not display 3
C. No error, code runs correctly
D. continue cannot be used inside for loops

Solution

  1. Step 1: Check syntax of continue

    In MATLAB, continue does not require a semicolon and is valid inside loops.
  2. Step 2: Understand loop behavior

    When i == 3, continue skips disp(i), so 3 is not displayed. This is expected behavior.
  3. Final Answer:

    No error, code runs correctly -> Option C
  4. Quick Check:

    continue valid in loops [OK]
Hint: Continue works without semicolon in loops [OK]
Common Mistakes:
  • Thinking semicolon is mandatory after continue
  • Believing continue is invalid in for loops
  • Expecting disp to print 3
5. You want to sum numbers from 1 to 10 but skip adding 5 and stop the loop if the sum exceeds 30. Which code correctly uses break and continue?
hard
A. sum = 0; for i = 1:10 if sum > 30 continue; elseif i == 5 continue; end sum = sum + i; end
B. sum = 0; for i = 1:10 if i == 5 break; elseif sum > 30 continue; end sum = sum + i; end
C. sum = 0; for i = 1:10 if sum > 30 continue; elseif i == 5 break; end sum = sum + i; end
D. sum = 0; for i = 1:10 if i == 5 continue; end sum = sum + i; if sum > 30 break; end end

Solution

  1. Step 1: Understand conditions order

    We must skip adding 5 (continue when i==5) and stop loop if sum > 30 (break after adding).
  2. 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.
  3. Final Answer:

    C -> Option D
  4. Quick Check:

    Skip 5 with continue; break after sum check [OK]
Hint: Continue before add to skip; break after add to stop [OK]
Common Mistakes:
  • Breaking when i==5 instead of continuing
  • Checking sum > 30 before adding
  • Using continue to skip after sum check
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard