Program for Switch case example.

0
03 Java Operators.
Program for Switch case example:
//SwitchDemo.java
class SwitchDemo
   {
static void m1(int a)
{
System.out.println("before switch");

switch(a)
{
case 1:
System.out.println("in case1 start");
System.out.println("in switch");
System.out.println("in switch");
System.out.println("in case 1 end");

case 2:

System.out.println("in case 2 start");
System.out.println("in switch");
System.out.println("in switch");
System.out.println("in case 2 end");
break;

case 3:

System.out.println("in case 3 start");
System.out.println("in switch");
System.out.println("in switch");
System.out.println("in case 3 end");
break;

                        default :

System.out.println("in default start");
System.out.println("in switch");
System.out.println("in switch");
System.out.println("in default end");
}

System.out.println("after switch");

}
public static void main(String[] args)
{
//m1(1);
/*
there is no break in case 1: hence case 1: and case 2: both will be executed

before switch

in case1 start
in switch
in switch
in case 1 end
in case 2 start
in switch
in switch
in case 2 end
after switch
*/

//m1(2);

/*
before switch
in case 2 start
in switch
in switch
in case 2 end
after switch
*/
m1(20);
m1(30);
/*
before switch
in default start
in switch
in switch
in default end
after switch
*/
}
}

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 Next

About the author

Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus. Aenean fermentum, eget tincidunt.

0 comments:

Recent Posts