0
0
MATLABdata~20 mins

Switch-case statements in MATLAB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Switch-Case Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple switch-case
What is the output of the following MATLAB code?
MATLAB
x = 3;
switch x
    case 1
        disp('One')
    case 2
        disp('Two')
    case 3
        disp('Three')
    otherwise
        disp('Other')
end
AThree
BTwo
COne
DOther
Attempts:
2 left
💡 Hint
Check which case matches the value of x.
Predict Output
intermediate
2:00remaining
Switch-case with string input
What will this MATLAB code display?
MATLAB
color = 'red';
switch color
    case 'blue'
        disp('Blue color')
    case 'red'
        disp('Red color')
    case 'green'
        disp('Green color')
    otherwise
        disp('Unknown color')
end
ABlue color
BRed color
CUnknown color
DGreen color
Attempts:
2 left
💡 Hint
Look at the value of the variable color.
Predict Output
advanced
2:00remaining
Switch-case with multiple values in one case
What does this MATLAB code print?
MATLAB
val = 5;
switch val
    case {1, 3, 5}
        disp('Odd number')
    case {2, 4, 6}
        disp('Even number')
    otherwise
        disp('Other number')
end
AOther number
BEven number
COdd number
DError
Attempts:
2 left
💡 Hint
Check which group contains the value 5.
Predict Output
advanced
2:00remaining
Switch-case with numeric and string mismatch
What error or output does this MATLAB code produce?
MATLAB
x = 2;
switch x
    case '2'
        disp('String two')
    case 2
        disp('Numeric two')
    otherwise
        disp('No match')
end
ANumeric two
BString two
CNo match
DError
Attempts:
2 left
💡 Hint
MATLAB distinguishes between strings and numbers in switch cases.
Predict Output
expert
3:00remaining
Switch-case with cell array and fall-through behavior
What is the output of this MATLAB code?
MATLAB
val = 'b';
switch val
    case {'a', 'b'}
        disp('First group')
    case {'b', 'c'}
        disp('Second group')
    otherwise
        disp('No group')
end
ASecond group
BError
CNo group
DFirst group
Attempts:
2 left
💡 Hint
MATLAB switch stops at the first matching case.