Source: https://mokxi.com/learn/arduino-switch-case
Updated: 2026-10-25

Learn

# Arduino switch case, with a light show to switch

Beginner

Written by the Mokxi team, updated October 25, 2026

Start building free Open the editor

Or see every lesson.

- 455 parts on the bench
- 43 boards running now
- 1.00x real time, on every board

Press Run, then press the button

Each press moves to the next show, and switch (show) picks which one draws the frame.

switch (mode) jumps straight to the case that matches the value of mode and runs from there until a break. It is a neater way to write a long if, else if, else if chain when you are checking one variable against a list of fixed values, such as a mode number, a menu choice or a letter typed into the serial monitor.

The circuit above is a ring of 16 color LEDs behind a star, on an Uno, with four light shows and a button. Press Run and the serial monitor prints "Star lit: show 0, warm twinkle". Press the button and it prints "Show 1" and a gold comet runs around the ring. Press again for "Show 2", a rainbow, then "Show 3", red and green, and the fifth press goes back to "Show 0".

The button only adds one to a number. A switch turns that number into a show. Below is how it works, what happens when you forget break, and the errors the compiler gives for the three things a switch will not do.

Want the step-by-step version? The lesson "One branch runs" walks through this with checkpoints.
Open the lesson

## The shape of a switch

Here is the end of the sketch running above. Each press does show = (show + 1) % SHOWS;, which counts 0, 1, 2, 3 and back to 0, because % is the remainder after dividing by 4. Then every frame, the switch looks at show and calls one of the four drawing functions.

Each case is followed by a fixed value and a colon. The sketch jumps to the matching case and runs the lines after it. break jumps out to the end of the switch. To add a fifth show, write one more function, add case 4: myShow(); break; and change SHOWS to 5.

firmware/uno/examples/treestar (the switch running above)

## Forget break and it falls through

Without a break, a case runs on into the next one. That is the most common switch bug. In the sketch below, case 0 has no break, so when m is 0 it printed "zero one": it ran case 0 and then carried straight on into case 1. When m was 1 it printed "one", for 2 "two", and for 3, which has no case of its own, default printed "other".

default is the case for everything else. It is optional, but worth writing even if all it does is print the value it did not expect, because a mode that is out of range is a bug you want to hear about.

A missing break

## Letters as cases, and two cases sharing one block

A char is a small number, so a switch works on letters too, written in single quotes. Falling through has a good use here: put two cases one after the other with nothing between them and they share the same lines. The sketch below answers r and R alike. Typed "rRx", it printed "red", "red" and "unknown: x", and the newline the serial monitor sends after every line has a case of its own that does nothing.

Ranges are not standard C++, but both the Arduino IDE’s compiler and Mokxi’s accept case 0 ... 9: as an extension, with spaces around the three dots. Checking 0, 15 and 30 against case 0 ... 9, case 10 ... 29 and default printed "cold", "mild" and "hot". For anything more than a couple of ranges, an if chain is clearer.

Commands with switch

## What a switch will not do, and the errors

A switch only works on whole numbers and characters. Switch on a String and the build stops: Mokxi’s clang says statement requires expression of integer type ('String' invalid), and the Arduino IDE’s GCC says switch quantity not an integer. A float gets the same error. For words, compare with if (cmd == "on"); for decimals, use if with < and >.

Declaring a variable straight after a case label stops the build too. case 0: int count = 3; followed by case 1: gave cannot jump from switch statement to this case label, with a note that the jump bypasses variable initialization; GCC says jump to case label and crosses initialization of 'int count'. Put the case’s lines in braces, case 0: { int count = 3; ... break; }, and the variable belongs to that case alone.

Two cases with the same value is an error as well: clang says duplicate case value '1' and GCC says duplicate case value. It usually means a line was copied and its number not changed.

## Questions

Can you use a switch case with strings in Arduino?

No. A switch only takes whole numbers and characters, so a String gives "switch quantity not an integer". Use an if and else if chain with == for words, or switch on the first letter.

What happens if I forget break in a switch case?

The sketch runs on into the next case and keeps going until it reaches a break or the end of the switch. With no break after case 0, a value of 0 runs case 0 and then case 1.

When should I use switch instead of if else?

When one variable is compared with a list of fixed values: modes, menu items, commands, states. Use if when you are testing ranges, several variables at once, or anything that is not a whole number.

How do I declare a variable inside a case?

Wrap the case in braces: case 0: { int count = 3; ... break; }. Without them, the compiler refuses with "jump to case label" in the Arduino IDE.

Can two cases run the same code?

Yes. Write the case labels one after the other with nothing in between, such as case 'r': case 'R':, and both run the lines that follow.

Related

## Keep going

Arduino State Machine: A Four-State Reaction Game Arduino Serial Read: Input from the Serial Monitor Arduino Functions: Write It Once, Call It Anywhere Arduino NeoPixel (WS2812B): Wiring and Code Arduino Christmas Lights to Build in Your Browser Arduino if else: One Branch Runs, and Why Arduino enum: Named States, enum class and Switch The tree topper star project Beginner WS2812B LEDs in the parts bin The project page, with the full sketch Beginner

Sources

## Where the facts on this page come from

- Arduino reference: switch...case
- Arduino reference: break

## Build this for real

Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.

Start building free Open the editor
