Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a number outside the loop range.
Using
continue instead of break.✗ Incorrect
The break statement stops the loop when i equals 5.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
break instead of continue.Choosing a number not in the loop range.
✗ Incorrect
The continue statement skips the rest of the loop body when i is 3, so 3 is not printed.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
< or == which do not stop at the right time.Not using a comparison operator.
✗ Incorrect
The loop should stop when the sum is greater than 10, so use >.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 for even check.
Stopping at the wrong number.
✗ Incorrect
mod(i, 2) == 0 checks for even numbers to skip, and i == 7 stops the loop.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of 1 for odd check.
Stopping at a number greater than 9.
✗ Incorrect
The code checks for odd numbers with mod(i, 2) == 1 and stops when i == 9.