Java Switch Case Control Statements with Program Examples

In this tutorial we will study and understand the working of Java Switch Case Control Statements and take a few examples. We will also understand the working and when to use Switch case and when to use the If-Else statement.

Java Switch Case Statement

The Java switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement. It is a multi-way branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. Basically, the expression can be byte, short, char, and int primitive data types.

Java Switch Case

Syntax –
Program Example –

Output
20
Java Switch Statement is fall-through

The java switch statement is fall-through. It means it executes all statement after first match if break statement is not used with switch cases.

Program Example –
Output
20
30
Not in 10, 20 or 30
Some Points to remember about Switch Case –
  • Switch case only operates with values and not expressions.
  • Switch case is fall through (you just saw that example above)
  • Switch case works only with Integral datatypes – short, byte, int, char and not with any other type like float, string etc.
Watch it on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *