0
0
MATLABdata~10 mins

Break and continue in MATLAB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to exit the loop when the value reaches 5.

MATLAB
for i = 1:10
    if i == [1]
        break;
    end
end
Drag options to blanks, or click blank then click option'
A5
B0
C10
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a number outside the loop range.
Using continue instead of break.
2fill in blank
medium

Complete the code to skip printing the number 3 using continue.

MATLAB
for i = 1:5
    if i == [1]
        continue;
    end
    disp(i)
end
Drag options to blanks, or click blank then click option'
A0
B1
C5
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using break instead of continue.
Choosing a number not in the loop range.
3fill in blank
hard

Fix the error in the code to stop the loop when the sum exceeds 10.

MATLAB
sumVal = 0;
for i = 1:10
    sumVal = sumVal + i;
    if sumVal [1] 10
        break;
    end
end
Drag options to blanks, or click blank then click option'
A==
B>
C<
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < or == which do not stop at the right time.
Not using a comparison operator.
4fill in blank
hard

Fill both blanks to create a loop that skips even numbers and stops at 7.

MATLAB
for i = 1:10
    if mod(i, 2) == [1]
        continue;
    end
    if i == [2]
        break;
    end
    disp(i)
end
Drag options to blanks, or click blank then click option'
A0
B1
C7
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 for even check.
Stopping at the wrong number.
5fill in blank
hard

Fill all three blanks to print only odd numbers less than 10 and stop at 9.

MATLAB
for i = 1:15
    if mod(i, [1]) == [2]
        disp(i)
        if i == [3]
            break;
        end
    end
end
Drag options to blanks, or click blank then click option'
A2
B1
C9
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of 1 for odd check.
Stopping at a number greater than 9.