Toolvito

Works on this device

Switch Case Generator

Type the value to test and the cases, choose a language, and get a switch, match or case statement with a default. Each language's rule for fall-through is followed and said with the result. Copy the code or download it as a file.

Enter your values

Your values

A variable name such as status or order.status. In PHP and Bash the $ is added for you.

The default case runs when no other case matches.

Cases

More options

Your values are worked out in this browser. They are not sent to a server.

Your result will show here

Fill in the values, then choose Make the code.

How to use the switch case generator

  1. Choose the Language. Some languages have two forms, such as PHP switch and PHP match.
  2. Type the Value to test, such as status or order.status, and choose whether the case values are text, whole numbers or names of constants.
  3. Fill in the Cases: one value, or several with commas between them, and the code to run if you want it. Leave the code empty to get a note in its place.
  4. Tick Fall through to the next case where a case should go on into the next one. The tick box is there only in languages that can do it.
  5. Keep Add a default case ticked to handle every other value. The code is written while you type and is shown with the colours of its language; copy it or download it. Copy and Download give the plain code, without colours.

Fall-through and the default in each language

Fall-through means that after the code of one case, the code of the next case runs too. Some languages do it unless you stop it, some never do it, and some do it only when you write it.

Fall-through and the default by language
Language and form Falls through by itself? An intended fall-through is written as Default
JavaScript and TypeScript (switch) Yes, unless the case ends with break or return // falls through (a comment) default:
Java (switch with case …:) Yes, unless the case ends with break, return or throw // falls through (a comment) default:
Java 14 and later (switch with case … ->) No Not possible default ->
C (switch) Yes, unless the case ends with break or return /* fall through */ (a comment) default:
C++ (switch) Yes, unless the case ends with break or return [[fallthrough]]; (C++17) default:
C# (switch) No: a case with code must end with break, return, throw or goto goto case value; or goto default; default:
Go (switch) No fallthrough (not in the last case) default:
Kotlin (when) No Not possible else ->
PHP (switch) Yes, unless the case ends with break or return // falls through (a comment) default:
PHP 8 and later (match) No Not possible default =>
Python 3.10 and later (match) No Not possible case _:
Bash (case) No: ;; ends a case ;& (Bash 4.0 and later) *)
SQL (CASE) No Not possible ELSE

How the code is written

  • Text values are written in the quotes of the language, with a backslash before a backslash or a quote mark where the language needs it. In SQL a quote mark is written twice, and in Bash a quote mark is written as '\''.
  • Whole numbers are written without leading zeros, so 007 is written as 7. Names of constants, such as RED, Color.Red or Color::Red, are written as you typed them.
  • In a C-like switch, several values of one case become case labels one under the other. Every section gets break;, unless its last line starts with return, throw, break, continue or goto, or the case falls through.
  • A value that is in two cases is refused: only the first case could ever be chosen, and C, C++, Java, C# and Go refuse it.
  • PHP match and SQL CASE take one expression for each case, so their code is one line without a ; at the end. An empty case gives null or NULL; in Python it gives pass.

Worked example

With the starting values (JavaScript, value to test status, text cases active, pending, invited and blocked, a default) the tool writes switch (status) { with case 'active':, then case 'pending': and case 'invited': one under the other, then case 'blocked':, each followed by a note and break;, and a default: section.

In Go, a case with the code prepare that falls through gets the line fallthrough under its code, and no case gets break. In C#, the same case gets goto case followed by the first value of the next case.

Limits you should know

  • The code is written as text. It is not run or compiled here, and your own code in the cases is not checked, so test it in your project.
  • A text value cannot hold a comma here, because commas separate the values of a case. At most 200 cases.
  • C and C++ cannot switch on text, and a Bash case compares text patterns, not constants; the tool says so.
  • To make the enum that a switch tests, use the Enum Generator.

Related tools