Reference+
Name
switch
Description
Works like an if else structure, but switch is more convenient when you need to select between three or more alternatives. Program controls jumps to the case with the same value as the expression. All remaining statements in the switch are executed unless redirected by a break. Only primitive datatypes which can convert to an integer (byte, char, and int) may be used as the expression parameter. The default is optional.
Examples
- int num = 1; switch(num) { case 0: println("Zero"); // Does not execute break; case 1: println("One"); // Prints "One" break; }
- char letter = 'N'; switch(letter) { case 'A': println("Alpha"); // Does not execute break; case 'B': println("Bravo"); // Does not execute break; default: // Default executes if the case names println("None"); // don't match the switch parameter break; }
- // Removing a "break" enables testing // for more than one value at once char letter = 'b'; switch(letter) { case 'a': case 'A': println("Alpha"); // Does not execute break; case 'b': case 'B': println("Bravo"); // Prints "Bravo" break; }
Syntax
- switch(expression)
- {
- case name:
- statements
- case name: // Optional
- statements // "
- default: // "
- statements // "
- }
Parameters
- expressionbyte, char, or int
- namebyte, char, or int
- statementsone or more statements to be executed

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.