0
0
MATLABdata~20 mins

If-elseif-else statements in MATLAB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
If-Else Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested if-elseif-else
What is the output of the following MATLAB code?
MATLAB
x = 5;
if x < 3
    disp('Less than 3')
elseif x < 7
    disp('Between 3 and 6')
else
    disp('7 or more')
end
ABetween 3 and 6
BLess than 3
C7 or more
DNo output
Attempts:
2 left
💡 Hint
Check which condition matches the value of x first.
Predict Output
intermediate
2:00remaining
Value of variable after if-elseif-else
What is the value of variable y after running this code?
MATLAB
a = 10;
if a > 15
    y = 1;
elseif a == 10
    y = 2;
else
    y = 3;
end
A3
B1
C2
DUndefined
Attempts:
2 left
💡 Hint
Check which condition matches the value of a.
Predict Output
advanced
2:00remaining
Output with multiple elseif conditions
What will be displayed when this code runs?
MATLAB
score = 75;
if score >= 90
    disp('Grade A')
elseif score >= 80
    disp('Grade B')
elseif score >= 70
    disp('Grade C')
else
    disp('Grade F')
end
AGrade F
BGrade B
CGrade A
DGrade C
Attempts:
2 left
💡 Hint
Check the conditions from top to bottom.
Predict Output
advanced
2:00remaining
Output with logical operators in conditions
What does this code display?
MATLAB
temp = 15;
if temp < 0
    disp('Freezing')
elseif temp >= 0 && temp <= 20
    disp('Cold')
else
    disp('Warm')
end
AFreezing
BCold
CWarm
DNo output
Attempts:
2 left
💡 Hint
Check the range that temp falls into.
Predict Output
expert
2:00remaining
Output of nested if-elseif-else with variable changes
What is the final value of z after this code runs?
MATLAB
x = 3;
y = 5;
if x > y
    z = x - y;
elseif x == y
    z = x + y;
else
    if y > 10
        z = y - x;
    else
        z = x + y;
    end
end
A8
B12
C2
DError
Attempts:
2 left
💡 Hint
Follow the logic step by step, including the nested if.