--- title: switch...case categories: [ "Structure" ] subCategories: [ "Control Structure" ] --- = switch...case statement // OVERVIEW SECTION STARTS [#overview] -- [float] === Description Like link:../if[if] statements, link:../switchcase[switch case] controls the flow of programs by allowing programmers to specify different code that should be executed in various conditions. In particular, a switch statement compares the value of a variable to the values specified in case statements. When a case statement is found whose value matches that of the variable, the code in that case statement is run. [%hardbreaks] The link:../break[break] keyword exits the switch statement, and is typically used at the end of each case. Without a break statement, the switch statement will continue executing the following expressions ("falling-through") until a break, or the end of the switch statement is reached. [%hardbreaks] [float] === Syntax [source,arduino] ---- switch (var) { case label1: // statements break; case label2: // statements break; default: // statements break; } ---- [float] === Parameters `var`: an *integer* variable whose value to compare with various cases. Any integer data type is allowed*, such as `byte`, `char`, `int`, `long`. `label1`, `label2`: constants. Any integer data type here is also allowed. *You can also use the `bool` data type when you specify just two switch cases. Note that you can also use negative values as input. [float] === Returns Nothing -- // OVERVIEW SECTION ENDS // HOW TO USE SECTION STARTS [#howtouse] -- [float] === Example Code [source,arduino] ---- switch (var) { case 1: // do something when var equals 1 break; case 2: // do something when var equals 2 break; default: // if nothing else matches, do the default // default is optional break; } ---- [%hardbreaks] -- // HOW TO USE SECTION ENDS // SEE ALSO SECTION BEGINS [#see_also] -- [float] === See also [role="language"] -- // SEE ALSO SECTION ENDS