Switch Statement in Java

by | May 26, 2021 | Java

Home » Java » Switch Statement in Java

Introduction

The Switch Statement in Java can be thought of as a branching statement that divides the workflow of the program and enables it to branch out to various parts. It is an easy way to dispatch the execution based on a certain condition as defined by the case statements. The condition is generally an expression that can be of any of the following forms, namely byte, short, char, and int primitive data types. The String Class and the Wrapper Class has also been included in the list with the introduction of JDK7.

Syntax of the Switch Case Statement:

// switch statement
switch (expression which can be of the above types as mentioned)
{
// case statements (conditional statements)
// The values must be of the same type as the expression.
case value1 :
// Set of Statements
break; // the break statement is optional.
case value2 :
// Set of Statements
break; // The break statement is optional.
case valuen :
//Set of Statements
break; //The break statement is optional.
//There is no limit to the number of case statements that you can code.
// The default statement is executed when neither of the case statements are satisfied.
// The default statement does not need to be accompanied with a break statement.
default :
// Statements
}

 

Some Important Rules for Switch Statements:

  • No case values can be repeated. Switch statements do not harbour duplicates.
  • As mentioned before, the switch variable (expression) and the case variables must be of the same data type.
  • All case values should either be constants or literals. Variables cannot be used in case statements.
  • The Break statement makes sure the switch case statement terminates when one of the case statements has been executed.
  • The Break statement is optional. If more than two case statements are satisfied and there is no break after each case, the execution will move on to the next statement. This is called Switch Case fall-through in Java.
  • The default can be placed anywhere in the switch block. It does not necessarily have to be at the end. When neither of the cases are satisfied or otherwise, the default statement is executed before exiting the switch block. If the default statement is not at the end, a break statement has to be put into place to avoid fall-through to other cases.

Example

//Java program to show how switch case works by implementing a switch case block
//int type expression has been used as the switch case expression
public class Demo {
public static void main (String [] args)
{
int day = 5;
String dayS;
switch (day)
{
case 1:
dayS = “Monday”;
break;
case 2:
dayS = “Tuesday”;
break;
case 3:
dayS = “Wednesday”;
break;
case 4:
dayS = “Thursday”;
break;
case 5:
dayS = “Friday”;
break;
case 1:
dayS = “Saturday”;
break;
case 1:
dayS = “Sunday”;
break;
default:
dayS = “Invalid Day”;
}
System.out.println (dayS);
}
}

 

OUTPUT

Friday

 

Example:

//Demo program without case statements
public class Demo {
public static void main (String [] args)
{
int day = 5;
String dayS;
switch (day)
{
case 1:
dayS = “Monday”;
break;
case 2:
dayS = “Tuesday”;
break;
case 3:
dayS = “Wednesday”;
break;
case 4:
dayS = “Thursday”;
break;
case 5:
dayS = “Friday”;
break;
case 1:
dayS = “Saturday”;
break;
case 1:
dayS = “Sunday”;
break;
default:
dayS = “Invalid Day”;
}
System.out.println (dayS);
}
switch (day)
{
case 1:
case 2:
case 3:
case 4:
case 5:
dayT = “Weekday”;
break;
case 6:
case 7:
dayT = “Weekend”;
break;
default:
dayT = “Invalid daytype”;
}
System.out.println (dayS + “is  a ” + dayT);
}
}

 

OUTPUT
Friday is a weekday

Author

0 Comments

Submit a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author