switch case in java - java switch

 

switch statement in java

switch case in java 

Introduction

Instead of using if-else if ladder we can use switch case statement.  The switch-case statement use when multiple option available. A switch-case work with primitive data types and it also work with enumerated types.

  • The case use as literal or constant.
  • Case must be unique
  • With JDK 7 and after word, String literal/constant is also use to control a switch statement.
  • The value passes from switch can be have any integer value after case keyword.
  • And can take any order of value.
  • It is mainly use in menu driven program

The syntax of Switch case statement

Switch (expression) {

  case 1:

    // code block

    break;

  case 2:

    // code block

    break;

case 3:

    // code block

    break;

case 4:

    // code block

    break;

 

  default:

    // code block

}

Switch-case statement generally use with break statement as it not pass boolean (true or false) value like in if else statement. In switch-case, expression pass, say int value 1 and case 1: is executed and then case 2: block execute till remaining case. And if no case will match then default block is use.

 We can explain the switch case with the following example

import java.util.*;

public class WeekDay

{

    public static void main(String args[])

    {

        Scanner sc=new Scanner (System.in);

        int day;

        System.out.println ("Enter 1-7 :");

        day=sc.nextInt();

        switch(day)

        {

            case 1:

            System.out.println ("Sunday");

            break;

            case 2:

            System.out.println ("Monday");

            break;

            case 3:

            System.out.println ("Tuesday");

            break;

            case 4:

            System.out.println ("Wednesday");

            break;

            case 5:

            System.out.println ("Thursday");

            break;

            case 6:

            System.out.println ("Friday");

            break;

            case 7:

            System.out.println ("Saturday");

            break;

            default:

            System.out.println ("Wrong Choice");

        }

    }

}

In the above program user enter 1 to 7 as day. If user press 1 it will print Sunday, if user press 2 it will print Monday and so on. Here when user press 1-7 as an input it passes as parameter from switch statement and match with the case, and if found the exact match it will print the match block and break from the switch case block. 

 Look into another example which is a menu driven program.

 import java.util.*;

public class MenuCalculator

{       

    // main method

    public static void main(String args[])

    {

        // menu method

        int num1, num2, add, sub, mul, div, choice;

        Scanner sc=new Scanner(System.in);

        // menu list

        System.out.println("Press 1 For Addition");

        System.out.println("Press 2 For Substraction");

        System.out.println("Press 3 For Multiplication");

        System.out.println("Press 4 For Division");

        System.out.println("Press 5 For Exit\n\n");

        System.out.println("Enter Your Choice");

        choice = sc.nextInt();

        switch(choice)

        {

            case 1:

            System.out.println("Enter two Numbers:");

            num1 = sc.nextInt();

            num2 = sc.nextInt();

            add = num1 + num2;  // addition

            System.out.println("Sum=" + add);

            break;

            case 2:

            System.out.println("Enter two Numbers:");

            num1 = sc.nextInt();

            num2 = sc.nextInt();

            sub= num1 - num2;   // Substraction

            System.out.println("Subs=" + sub);

            break;

            case 3:

            System.out.println("Enter two Numbers:");

            num1 = sc.nextInt();

            num2 = sc.nextInt();

            mul = num1 * num2;  // multiplication

            System.out.println("Mult=" + mul);

            break;

            case 4:

            System.out.println("Enter two Numbers:");

            num1 = sc.nextInt();

            num2 = sc.nextInt();

            div = num1 / num2;  // division

            System.out.println("Div=" + div);

            break;

            case 5:

            System.out.println("Thank you for using me");

            System.exit(0);    // exit from program

            default:

            System.out.println("Wrong Choice, please press(1-5)");

        }

    }

}

Conclusion in java switch

So switch case statement in java is used to transfer control to a specified block of code, which is based on the value provided by the user. The value provided by the user can based on int, String, enums, char etc.

More Java program


SHARE THIS
Previous Post
Next Post