Java Switch Statement



User Rating:  / 0
PoorBest 
Details

Java Switch Statement :

View source
package com.developerpages.snippets.basics;
 
 
 
public class SwitchStatementExample {
 
    public static void main(String[] args) {
 
        for (int i = 0; i <= 4; i++) {
            switch (i) {
                case 0:
                    System.out.println("case 0 , i = " + i);
                    break;
                case 1:
                    System.out.println("case 1 , i = " + i);
                    break;
                case 2:
                    System.out.println("case 2 , i = " + i);
                    break;
                default:
                    System.out.println("i > 2");
            }
        }
    }
}
 

output :

case 0 , i = 0
case 1 , i = 1
case 2 , i = 2
i > 2
i > 2

Free Flowing Switch Statement (without break statement):

View source
package com.developerpages.snippets.basics;
 
public class FreeFlowingSwitchStatement {
 
    public static void main(String[] args) {
 
        int i = 0;
        switch (i) {
            case 0:
                System.out.println("case 0 , i = " + i);
            case 1:
                System.out.println("case 1 , i = " + i);
            case 2:
                System.out.println("case 2 , i = " + i);
            default:
                System.out.println("Free flowing switch");
        }
    }
}

output:

case 0 , i = 0
case 1 , i = 0
case 2 , i = 0
Free flowing switch



You have no rights to post comments

   

Login  

   

     

© Developerpages