0
0
MATLABdata~10 mins

If-elseif-else statements 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 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'
Ax
By
Cz
Dnum
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name other than x.
Forgetting to write the variable before the comparison.
2fill in blank
medium

Complete 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'
Ay
Bx
Cnum
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using assignment = instead of equality ==.
Using a wrong variable name.
3fill in blank
hard

Fix 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'
Aend
Belseif
Celse if
Delse
Attempts:
3 left
💡 Hint
Common Mistakes
Writing elseif without a condition.
Writing else if as two words.
Using end instead of else.
4fill in blank
hard

Fill 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'
A>
B<
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators like < or !=.
Mixing up the order of conditions.
5fill in blank
hard

Fill 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'
Ax
C'Negative number'
D'Negative'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in conditions.
Forgetting to put quotes around the message string.