Skip to main content
Logo image

Subsection Switch Statement

The switch statement is another option for controlling flow in MATLAB. It is best when you are comparing one value to a list of discrete, exact matches.
A switch statement compares a variable to the values listed in each case. The first match runs, and MATLAB skips the rest.

πŸ“

The switch Statement Structure.

Use Case:
  • if var is equal to val1, run blockA,
  • if var is equal to val2 or val3, run blockB,
  • if var is equal to val4, run blockC,
  • if var was not matched, run blockD (default).
switch var

	case val1          % var == val1? YES ➜ run blockA 
		               %              NO  ➜ check next case
		blockA
	case {val2, val3}  % var == val2 OR val3? Yes ➜ run blockB
		               %                      NO  ➜ check next case
		blockB
	case val4          % var == val4? Yes ➜ run blockC
		               %              NO  ➜ check next case
		blockC
	otherwise          % no matches? YES ➜ run blockD
		               %             NO  ➜ check next case
		blockD
end
Rules:
  • Case values are checked in order from top to bottom.
  • The otherwise block runs only when no cases match.
  • The otherwise (default) branch is optional.
  • When there is only one case value, curly braces { } are optional.
  • Every switch-statement must end with end.
  • Case values can be numbers, strings, or other data types that support equality comparisons.
  • Case values are case-sensitive when comparing strings.
  • Case values must be unique; duplicate case values will cause an error.
Limitations:
  • Case values cannot use logical operators (e.g., <, >, ==, &, |).
  • Switch statements are not suitable for range-based conditions (e.g., checking if a value is between two numbers).
The switch-case structure is similar to the if structure, but offers a few advantages. First, it is easier to read; second, it is better when comparing strings (of possibly different lengths).
As an example, let’s check whether a cadet is in their first two years at VMI.
The cases are grouped by curly brackets so that a case will be satisfied if the value of Year is any of the values in a specific case. Once this code is executed, the switch command will attempt to match value of Year.

Checkpoint 30.

(a)

(i) Switch keywords and roles.
    Match each switch keyword to its role.
    A switch block tests one expression, each case provides a value to match, and otherwise provides the default path before end closes the block.
  • switch
  • Starts the block and names the expression to compare.
  • case
  • Runs when the expression matches a listed value.
  • otherwise
  • Runs when no case matches.
  • end
  • Closes the switch statement.

(b) Switch vs if-elseif for exact matches.

When is a switch statement more suitable than an if-elseif-else chain? Select all that apply.
  • When comparing a single variable to multiple exact values.
  • The switch statement is designed for testing one variable against a list of specific values.
  • When you need to test ranges like x > 10.
  • Switch statements cannot use logical operators or ranges; use if-elseif for range-based conditions.
  • When comparing string values of different lengths.
  • Switch statements handle string comparisons cleanly, especially when strings have different lengths.
  • When the case values need to be computed at runtime.
  • Case values must be hard-coded constants, not variables or expressions.

(c) Otherwise branch is optional in switch.

    The otherwise branch in a switch statement is optional.
  • True.

  • While the otherwise branch is optional, including it provides a default action when no cases match, which can help prevent unintended behavior.
  • False.

  • While the otherwise branch is optional, including it provides a default action when no cases match, which can help prevent unintended behavior.