Challenge - 5 Problems
If-Else Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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
Attempts:
2 left
💡 Hint
Check which condition matches the value of x first.
✗ Incorrect
Since x = 5, the first condition (x < 3) is false, but the second (x < 7) is true, so it prints 'Between 3 and 6'.
❓ Predict Output
intermediate2: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
Attempts:
2 left
💡 Hint
Check which condition matches the value of a.
✗ Incorrect
a equals 10, so the second condition (a == 10) is true, setting y to 2.
❓ Predict Output
advanced2: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
Attempts:
2 left
💡 Hint
Check the conditions from top to bottom.
✗ Incorrect
75 is not >= 90 or 80, but it is >= 70, so it prints 'Grade C'.
❓ Predict Output
advanced2: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
Attempts:
2 left
💡 Hint
Check the range that temp falls into.
✗ Incorrect
15 is between 0 and 20 inclusive, so it prints 'Cold'.
❓ Predict Output
expert2: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
Attempts:
2 left
💡 Hint
Follow the logic step by step, including the nested if.
✗ Incorrect
x (3) is not greater than y (5), and not equal, so else runs. Inside else, y (5) is not greater than 10, so z = x + y = 8.