Switch Conditional Statements

Authored by AllisonP

JavaScript Switch Statements

A switch statement allows a program to evaluate an expression by attempting to match the expression's value to a case label. If a match is found, the program jumps to the statement(s) associated with the matched label and continues executing at that point. Note that execution will continue sequentially through all the statements starting at the jump point unless there is a call to break;, which exits the switch statement. A switch statement looks like this:

switch (expression) {
    case label1:
        statement1;
        break;
    case label2:
        statement2;
        break;
    case label3:
        statement3;
        statement4;
        break;
    default:
        statement;
}

The program first looks for a case clause with a label matching the value of , then transfers control to the matching clause and executes the associated statements. If no matching label is found, the program looks for the optional default clause and, if found, transfers control to that clause and executes the statements associated with it. If no default clause is found, the program continues executing after the end of the switch statement.

The default Clause

By convention, the default clause is always listed last. This is because the statements are checked sequentially, so you run into the following issues if you use the default label in an earlier clause:

  • If the default case is listed before (above) a case that matches , it will match the default case instead. This means the statements associated with the programmed match case won't be executed.
  • If the default case doesn't have a break statement, any statements in the case label immediately following it will be executed.

The break; Statement

The break statement is optional, but you'll typically see one at the end of each case clause to ensure that the program breaks out of the switch statement once the statements associated with a matched case are executed. Once the flow of execution hits break;, it exits the switch statement and continues executing at the next line following the end of the switch statement; if the break statement is omitted, the program continues executing the next statement in the switch statement — even if its case label doesn't match .

EXAMPLE
Given an integer, , such that , do the following:

  1. If is equal to , print A.
  2. If is equal to , print B.
  3. If is equal to , print C.
  4. If is equal to , print D
  5. For all the other values of , print E.

Input Format

A single integer denoting .

Run
Input
Output

Run the code above with the given input, and then try replacing that input with other integers and seeing how it changes. Note that, once reached, the break; statements transfer control back outside of the switch statement to the next line of code (in this example, there is no more code to execute).

Now, let's consider the same problem, but this time we'll remove all the break; statements from our code:

Run
Input
Output

Run the code above with the given input, and then try replacing that input with other integers and seeing how it changes. Observe that the statements execute sequentially, starting with the matching case.

Now, let's look at what happens if we don't parse the input as an integer:

Run
Input
Output

Run the code above with the given input, and observe that the code does not parse as an integer. This means it's evaluated as an object, where a strict comparison (===) is made comparing the case label to the expression value.

Multi-Criteria Case

In the example below, we consider a similar problem in which there are multiple criteria for each case.

EXAMPLE
Given an integer, , such that , do the following:

  1. If is equal to , print A.
  2. If is equal to , print A.
  3. If is equal to , print A.
  4. If is equal to , print B.
  5. If is equal to , print B.
  6. If is equal to , print B.
  7. For all other values of , print C.

Input Format

A single integer denoting .

Run
Input
Output

Run the code above with the given input, and then try replacing that input with other integers and seeing how it changes.

If-Else Conditional StatementsRecommended Article
View

If-Else Conditional Statements

 
Go to Top
  1. Challenge Walkthrough
    Let's walk through this sample challenge and explore the features of the code editor.1 of 6
  2. Review the problem statement
    Each challenge has a problem statement that includes sample inputs and outputs. Some challenges include additional information to help you out.2 of 6
  3. Choose a language
    Select the language you wish to use to solve this challenge.3 of 6
  4. Enter your code
    Code your solution in our custom editor or code in your own environment and upload your solution as a file.4 of 6
  5. Test your code
    You can compile your code and test it for errors and accuracy before submitting.5 of 6
  6. Submit to see results
    When you're ready, submit your solution! Remember, you can go back and refine your code anytime.6 of 6
  1. Check your score