Javatpoint Logo
Javatpoint Logo

Switch Case Statement

Switch case is a control statement which is used to implement decision making logic. It comprises of cases which are independent decision branches. It is better substitute of if statements which has lots of conditions and cache memory issues.

C Switch Case Statement

To create switch case, C language provides switch, case and default keywords. Syntax of switch is given below.

C/C++/Java/C#/Javascript Switch Case Statement Syntax

Points to Remember

  • Switch case allows only integer and character constants in case expression. We can't use float values.
  • It executes case only if input value matches otherwise default case executes.
  • Break keyword can be used to break the control and take out control from the switch. It is optional and if not used, the control transfer to the next case.
  • Switch case is very useful while developing menu driven applications.
  • Switch cases can't have variable expressions ex. (a+2*b+3).
  • No duplicate case expression is allowed.

Java Switch Case Statement

Java switch is simillar to the C language switch statement and provides the same functionalities. In Java, the switch expression can be of byte, short, int, char, String and Enum type.

The type of switch expression must match to the cases type, otherwise it will generate an error.


Ruby Switch Case Statement

The switch case in Ruby, has different identifiers and syntax for declaration. The case keyword is used instead of switch, when instead of case and else instead of default.

Ruby uses === operator to match case and when expressions. Else expression executes if no when is matched.

Compares the expression specified by case and that specified by when using the === operator and executes the code of the when clause that matches.

Ruby Switch Case Syntax


Rust Switch Case Statement (Pattern Matching)

Rust does not support C style switch case but has a better alternate match expression. Match expression is a concept which works on pattern matching and has brances to match the expression.

It follows value => expression syntax and match expression match to each value and execute the corresponding expression. The syntax is given below.

Rust Pattern Matching Syntax

We can match against literals directly, and _ acts as an 'any' case.


Swift Switch Case Statement

Swift's switch statement considered as more powerful than C type switch case. It is used to match various types of cases including range, tuple etc.

In swift, switch cases don't fall through to the lower case. The syntax of switch case is given below.

Swift Switch Case Syntax

Let's see an example of switch case which prints the name of the digit we pass.


C Switch Case Example 1 : Integer Input

Output:

Two

The above example prints Two based on the input and due to break it stops further execution and exit the switch block.


Java Switch Case Example 1 : Integer Input

Output:

Two

Ruby Switch Case Example : Integer Input

Output:

Two

C# Switch Case Example : Integer Input

Output:

Two

Php Switch Case Example : Integer Input

Output:

Two

Rust Switch Case Example : Integer Input

Output:

Two

Javascript Switch Case Example : Integer Input

Output:

Two

Swift Switch Case Example : Integer Input


C Switch Case Example : Integer Expression

It allows us to use integer expressions with cases. See the below example.

Output:

Three

Java Switch Case Example: Integer Expression

Output:

Three

Ruby Switch Case Example: Integer Expression

Output:

Three

C# Switch Case Example: Integer Expression


Php Switch Case Example: Integer Expression

Output:

Three

Rust Switch Case: Integer Expression

Rust does not allow integer expressions into the match expression. So applying 1+2 or a+3 etc will generate error in the code.

Rust Switch Case: Integer Expression

Output:

Three

Swift Switch Case: Integer Expression


C Switch Case Example: Character Constants

Integer or character constants both are supported by the switch case. A character is replaced by it's ascii value.

Note: C compiler implicitly replaces character constants into it's ASCII values, 'a' => 97.

Output:

It's a

Java Switch Case Example: Character Constants

Integer or character constants both are supported by the switch case. A character is replaced by it's ascii value.

Note: Java implicitly replaces character constants into it's ASCII values, 'a' => 97.

Output:

It's a

Ruby Switch Case Example: Character Constants

Output:

It's a

C# Switch Case Example: Character Constants

Output:

It's a

PHP Switch Case Example: Character Constants

Output:

It's a

Rust Switch Case Example: Character Constants

Output:

It's a

Javascript Switch Case Example: Character Constants

Output:

It's a

Swift Example to match single character

Output:

It's a

C Switch Case Example: Without Break

Output:

Two 
Three 
value is other than 1, 2, 3

See, in the above example, an integer input is passed which matches to the case 2 and executes not only it but also all the other cases comes after this case including the default.

It means without break it executes other cases also until end of the switch is reached or a break is found.


Java Switch Case Example : Without Break

Output:

Two
Three
value is other than 1, 2, 3

Ruby Switch Case Example : Break

Ruby does not use break statement and in switch case it uses implicit break which does not require to add extra break.


C# Switch Case Example : Break

C# does not allow implicitly fall through from one case to another. Either we can put break explicitly or C# uses implicitly break to prevent automatic fall through from one case to another.

If we does not put break and compile the code, C# reports an error:

Error: Control cannot fall through from one case label to another.


PHP Switch Case Example : Without Break

Without break, it falls through all the cases until switch completes or finds a break.

Output:

It's a
It's b
It's c
value is other than a, b, c

Javascript Switch Case Example : Without Break

It falls through to last case or till the break found. It executes all the cases after the matched case. See the example below.

Output:

It's a
It's b
It's c
value is other than a, b, c

Swift Switch Case Example : Without Break

It does not fall through even break statement is removed. It means swift does not allow fall through cases to lower in the switch.


Somethimes we want to execute common statements for multiple cases. See an example below.


C Switch Case Statement Example : Common Statements

Output:

It's a

Multiple cases are combined to execute the common tasks. Whether input is 'a' or 'A' it will print the same message (execute the common statements).


Java Switch Case Example: Common Statements

Output:

It's a

Ruby Switch Case Example: Common Statements

Ruby does not work with this scenario because it breaks each when implicitly and takes control out of the case.


C# Switch Case Example: Common Statements

Output:

It's a

Php Switch Case Example: Common Statements

Output:

It's a

Rust Switch Case Example: Common Statements

Executing common statements or mutiple patterns is pretty easy and be achived using | (or) operator.

Output:

It's a

Javascript Switch Case Example: Common Statements


Swift Switch Case Example: Common Statements

It requires at least one executable statement in the case, otherwise throws an error message. See the example below.

Output:

error: 'case' label in a 'switch' should have at least one executable statement.

To achieve it, Swift provides a new concept the use of , (comma) to provide multiple cases. See an example below.

Output:

It's a

Java Switch Case Example: String Input

Output:

Sunday

Ruby Switch Case Example: String Input

Output:

Sunday

C# Switch Case Example: String Input

Output:

Sunday

Php Switch Case Example: String Input


Rust Switch Case Example: String Input

Output:

Sunday

Javascript Switch Case Example: String Input

Output:

Sunday

Swift Switch Case Example: String Input

Output:

Sunday

Ruby Switch Case Example: Input Range

When expression is given in range and the input is matche within specified range and executes code. See the example below.

Output:

little child

Rust Switch Case Example: Input Range

Pattern value can lie between the range and mached to evaluate expression.

Output:

little child

c, c++, java, c#, ruby, php, rust, javascript, python, swift

Next Topic#





Help Others, Please Share

facebook twitter pinterest