0
0
MATLABdata~20 mins

Break and continue in MATLAB - Practice Problems & Coding Challenges

Choose your learning style9 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).