0
0
MATLABdata~10 mins

Switch-case 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 start a switch-case statement for variable color.

MATLAB
switch [1]
    case 'red'
        disp('Stop');
    otherwise
        disp('Go');
end
Drag options to blanks, or click blank then click option'
Avalue
Bcase
Cif
Dcolor
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'case' instead of the variable name after switch.
Using 'if' which is a different control structure.
2fill in blank
medium

Complete the code to display 'Go' when the color is not 'red'.

MATLAB
switch color
    case 'red'
        disp('Stop');
    [1]
        disp('Go');
end
Drag options to blanks, or click blank then click option'
Aelse
Bcase 'green'
Cotherwise
Ddefault
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'else' which is not valid in MATLAB switch-case.
Using 'default' which is not a MATLAB keyword.
3fill in blank
hard

Fix the error in the switch-case syntax by completing the missing keyword.

MATLAB
switch day
    [1] 'Monday'
        disp('Start of week');
    case 'Friday'
        disp('End of week');
end
Drag options to blanks, or click blank then click option'
Acase
Bif
Cswitch
Dotherwise
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the 'case' keyword before the condition.
Using 'if' inside switch-case which is incorrect.
4fill in blank
hard

Fill both blanks to create a switch-case that displays 'Weekend' for Saturday and Sunday.

MATLAB
switch day
    [1] 'Saturday'
        disp('Weekend');
    [2] 'Sunday'
        disp('Weekend');
    otherwise
        disp('Weekday');
end
Drag options to blanks, or click blank then click option'
Acase
Botherwise
Dswitch
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'otherwise' or 'switch' instead of 'case' for conditions.
5fill in blank
hard

Fill all three blanks to create a switch-case that prints the day type based on the variable day.

MATLAB
switch [1]
    case 'Saturday'
        disp([2]);
    case 'Monday'
        disp([3]);
    otherwise
        disp('Midweek');
end
Drag options to blanks, or click blank then click option'
Aday
B'Weekend'
C'Start of week'
Dcolor
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong variable name in the switch statement.
Not using quotes around the strings to display.