Java Control Statement: Switch Case

switch

Switch Statement

A switch case statement is used when different actions are to be executed under different set conditions. In this scenario a variable is evaluated for equality against a list of values(cases) . If the condition returns true that corresponding case is executed. If it is false it skips all the conditions in between and either go to next case and execute it or goes to default and terminates.

Understanding the Concept

The following rules apply to a switch statement −

  • The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.

  • You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.

  • The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.

  • When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.

  • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.

  • Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.

  • A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Let us understand this working with the help of this flow chart given below:-
switch statement

Syntax of switch statement

switch (expression) {
  case value1:
    //Statements executed when the
    //result of expression matches value1
    [break;]
  case value2:
    //Statements executed when the
    //result of expression matches value2
    [break;]
  ...
  case valueN:
    //Statements executed when the
    //result of expression matches valueN
    [break;]
  [default:
    //Statements executed when none of
    //the values match the value of the expression
    [break;]]
}

Let us understand the concept of switch with the help of an Example. Consider that we want to print a month name from a set of all the month names. We do not want the rest of the list to be printed, hence we use the switch case. The code for the same is given as follows-

Run
public class Main 
{
    public static void main(String[] args) 
    {
    
        int month = 6;
        String monthname;
        switch (month) {
            case 1: monthname = "January";
            break;
            case 2: monthname = "February";
            break;
            case 3: monthname = "March";
            break;
            case 4: monthname = "April";
            break;
            case 5: monthname = "May";
            break;
            case 6: monthname = "June";
            break;
            case 7: monthname = "July";
            break;
            case 8: monthname = "August";
            break;
            case 9: monthname = "September";
            break;
            case 10: monthname = "October";
            break;
            case 11: monthname = "November";
            break;
            case 12: monthname = "December";
            break;
            default: monthname = "Invalid month";
            break;
        }
       System.out.println("Hey Prepster, Happy " + monthname);
    }
}

Output:

Hey Prepster, Happy June.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription