Switch and Case: Named Alternatives

Choose a message from a club code

Students select a club code: art, music or sport. The school wants a different room instruction for each named choice, with a clear message for an unknown code.

A switch evaluates a value and selects a matching case. In JavaScript, case matching uses strict equality. Execution can continue into following cases unless a control statement stops it.

  1. Match a case

    Explain which club branch a value selects.

  2. Prevent fall-through

    Use break for separate alternatives and understand intentional grouped cases.

  3. Handle unknowns

    Provide a useful default and know when if is clearer.

Match music to its room

  1. Read the code

    The chosen value is music.

    music
  2. Match a case

    The music label equals the chosen value.

    Room 2
  3. Leave the switch

    Break prevents the next case from running.

    Continue after switch
Read these stages alongside the complete example. They explain the relationship; they do not execute code.

Cases match values rather than numeric ranges

Use switch when one value has several discrete alternatives. It is often clearer than repeating equality checks against the same club code. For mark ranges, an ordered if chain is usually more direct.

break leaves the switch. default supplies a fallback when no case matched. Matching text is case-sensitive, so “Music” and “music” are distinct unless input is deliberately normalized.

Before starting, review conditional branches. These examples use JavaScript; other languages can use different syntax and rules.

Send the student to the selected club room

The chosen value matches music. The program prints Room 2 and break exits the switch, skipping the sport and default instructions.

The art case is not executed simply because it appears first; switch selects the matching entry point.

JavaScript · local teaching example

const club = 'music';
switch (club) {
  case 'art':
    console.log('Room 1');
    break;
  case 'music':
    console.log('Room 2');
    break;
  case 'sport':
    console.log('Sports field');
    break;
  default:
    console.log('Choose a listed club');
}

Expected output

Room 2

See what a missing break actually does

After a match, JavaScript executes following statements until it leaves the switch. It does not test every following label as a new condition. Missing break here prints both club messages.

Intentional fall-through can group multiple case labels before one shared action. Comment deliberate fall-through when readers might otherwise think it is a mistake.

JavaScript · local teaching example

const club = 'art';
switch (club) {
  case 'art':
    console.log('Room 1');
  case 'music':
    console.log('Room 2');
    break;
}

Expected output

Room 1
Room 2

Apply the rule to a small change

Write a switch for an unknown club code science with art and music as the only listed choices.

Worked answer

No listed case matches science, so default prints Choose a listed club. It is better than leaving the student without instructions.

JavaScript · local teaching example

const club = 'science';
switch (club) {
  case 'art': console.log('Room 1'); break;
  case 'music': console.log('Room 2'); break;
  default: console.log('Choose a listed club');
}

Expected output

Choose a listed club

Recognize mistakes and limits

A switch case with numeric 1 does not match string “1” under JavaScript strict equality. Normalize input deliberately rather than expecting a match.

Use braces around a case’s declarations when separate cases need their own lexical scope. JavaScript switch is not identical to pattern matching in other languages. Do not use it to obscure range checks or complicated combined conditions.

For the language or tool rules used here, see JavaScript control flow.

Check the core idea

JavaScript · code for this question

const club = 'music';
switch (club) {
  case 'art':
    console.log('Room 1');
    break;
  case 'music':
    console.log('Room 2');
    break;
  case 'sport':
    console.log('Sports field');
    break;
  default:
    console.log('Choose a listed club');
}
Why does the normal club example print only Room 2?
Choose one answer. You can try again.

Answer and explanation

The matching case prints its message and break leaves the switch.

Yes. Break prevents subsequent case statements from running.

Apply the idea to a new situation

JavaScript · code for this question

const club = 'art';
switch (club) {
  case 'art':
    console.log('Room 1');
  case 'music':
    console.log('Room 2');
    break;
}
What happens after a matched case without break?
Choose one answer. You can try again.

Answer and explanation

Execution continues into following statements until it leaves the switch.

Yes. That is why both messages appear here.

Match a value and control where execution stops

Switch is useful for named alternatives. Use break for separate cases, default for unmatched input, and intentional fall-through only when it makes the rule clearer.

Connect this idea with strict equality and range-based decisions.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Truthy and Falsy Values in Conditions →