Switch Case Structure in Java

In Java, the switch case structure is used for selection operations among various alternatives.

switch (variable name) {

     The value of the case variable:

         actions to be taken when this process is entered

         break;

    default:

        if none of the case transactions,

        break;

}

 

example:

public class Main
{
	public static void main(String[] args) {
		int count =1;
		while(count<=4){
    		switch(count) {
    		    case 1:
    		        System.out.println("case: 1 count :"+count);
    		        break;
    		    case 2:
    		        System.out.println("case: 2 count :"+count);
    		        break;
    		    default:
    		        System.out.println("case: default count :"+count);
    		        break;
            }
            count++;
        };
	}
}

 

output:

case: 1 count :1
case: 2 count :2
case: default count :3
case: default count :4

 

Tagged In:

Software developer

26 Total Posts