Press enter to see results or esc to cancel.


Java Switch Statement source code


Java Switch statement executes if any of the cases in the switch is matched. Just like if-else-if ladder statement.
Switch statements can be used only to String,int and enum parameters by default.

1.Java Switch with Integer Argument example.
Java_Switch.java


package com.chillyfacts.com;
public class Java_Switch {
    public static void main(String[] args) {
        int i = 100;
        switch (i) {
            case 5:
                System.out.println("value is 5");
                break;
            case 50:
                System.out.println("value is 50");
                break;
            case 100:
                System.out.println("value is 100");
                break;
            case 200:
                System.out.println("value is 200");
                break;
            case 300:
                System.out.println("value is 300");
                break;
            case 400:
                System.out.println("value is 400");
                break;
            case 500:
                System.out.println("value is 500");
                break;
            default:
                System.out.println("Not matching");
                break;
        }
        System.out.println("Done");
    }
}

Output

value is 100
Done

2.Java Switch with String Argument example.
Java_Switch_String.java


package com.chillyfacts.com;
public class Java_Switch_String {
    public static void main(String[] args) {
        String name = "Messi";
        switch (name) {
            case "Ronaldo":
                System.out.println("value is Ronaldo");
                break;
            case "Messi":
                System.out.println("value is Messi");
                break;
            case "Neymar":
                System.out.println("value is Neymar");
                break;
            case "Rooney":
                System.out.println("value is Rooney");
                break;
            case "Zlatan":
                System.out.println("value is Zlatan");
                break;
        }
        System.out.println("Done");
    }
}

Output

value is Messi
Done

3. Switch statement without Break statement is is fall-through. It will execute the case which is matched and executes all the remaining cases under that.
Java_Switch.java


package com.chillyfacts.com;
public class Java_Switch {
    public static void main(String[] args) {
        int i = 100;
        switch (i) {
            case 5:
                System.out.println("value is 5");
            case 50:
                System.out.println("value is 50");
            case 100:
                System.out.println("value is 100");
            case 200:
                System.out.println("value is 200");
            case 300:
                System.out.println("value is 300");
            case 400:
                System.out.println("value is 400");
            case 500:
                System.out.println("value is 500");
            default:
                System.out.println("Not matching");
        }
        System.out.println("Done");
    }
}

Output.

value is 100
value is 200
value is 300
value is 400
value is 500
Not matching
Done

Comments

Leave a Comment