Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the variable x is positive.
MATLAB
if [1] > 0 disp('Positive number'); end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name other than x.
Forgetting to write the variable before the comparison.
✗ Incorrect
The variable x is used to check if it is greater than zero.
2fill in blank
mediumComplete the code to add an elseif condition checking if x is zero.
MATLAB
if x > 0 disp('Positive number'); elseif [1] == 0 disp('Zero'); end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using assignment
= instead of equality ==.Using a wrong variable name.
✗ Incorrect
The elseif condition must check if x equals zero.
3fill in blank
hardFix the error in the else statement syntax.
MATLAB
if x > 0 disp('Positive'); elseif x == 0 disp('Zero'); [1] disp('Negative'); end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Writing
elseif without a condition.Writing
else if as two words.Using
end instead of else.✗ Incorrect
The correct keyword for the last condition is else without any condition.
4fill in blank
hardFill both blanks to complete the if-elseif-else structure checking if x is positive, zero, or negative.
MATLAB
if x [1] 0 disp('Positive'); elseif x [2] 0 disp('Zero'); else disp('Negative'); end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators like
< or !=.Mixing up the order of conditions.
✗ Incorrect
The first condition checks if x is greater than zero, the second if x equals zero.
5fill in blank
hardFill all three blanks to create a nested if-elseif-else that checks if x is positive, zero, or negative and prints a message accordingly.
MATLAB
if [1] > 0 disp('Positive'); elseif [2] == 0 disp('Zero'); else disp([3]); end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in conditions.
Forgetting to put quotes around the message string.
✗ Incorrect
The conditions check variable x. The else prints 'Negative'.