Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a while loop that runs as long as x is less than 5.
MATLAB
x = 1; while [1] disp(x); x = x + 1; end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'x > 5' which never becomes true initially.
Using 'x == 5' which runs only when x equals 5.
Using 'x >= 5' which is false at start.
✗ Incorrect
The condition x < 5 makes the loop run while x is less than 5.
2fill in blank
mediumComplete the code to stop the loop when the variable 'count' reaches 10.
MATLAB
count = 0; while [1] count = count + 1; disp(count); end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'count == 10' which runs only when count equals 10.
Using 'count > 10' which is false at start.
Using 'count ~= 10' which runs when count is not 10, causing an extra iteration.
✗ Incorrect
The loop continues while count < 10, stopping when count reaches 10.
3fill in blank
hardFix the error in the while loop condition to avoid an infinite loop.
MATLAB
num = 5; while [1] disp(num); num = num - 1; end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'num == 5' which never changes inside the loop.
Using 'num >= 5' which is true initially but false after decrement.
Using 'num < 0' which is false at start.
✗ Incorrect
The condition num > 0 ensures the loop stops when num reaches 0, preventing infinite looping.
4fill in blank
hardFill both blanks to create a while loop that prints numbers from 1 to 5.
MATLAB
i = 1; while [1] disp(i); i = i [2] 1; end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i < 5' which stops before printing 5.
Using '-' operator which decreases i causing infinite loop.
✗ Incorrect
The loop runs while i <= 5 and increments i by 1 each time.
5fill in blank
hardFill all three blanks to create a while loop that sums numbers from 1 to 5.
MATLAB
sum = 0; i = 1; while [1] sum = sum + i; i = i [2] 1; end result = sum [3] 15;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '<=' causing missing last number.
Using '-' operator decreasing i causing infinite loop.
Using '=' instead of '==' for comparison.
✗ Incorrect
The loop runs while i <= 5, adds i to sum, increments i by 1, and checks if sum equals 15.