Write a programme that show simple example of switch

Category : Java | Sub Category : Control Statement | By Runner Dev Last updated: 2020-09-28 19:15:34 Viewed : 212


Write a programme that show simple example of switch


Source Code: SwitchSample.java



 /* Write a programme that show simple example of switch

 */

 

public class SwitchSample { // class definition start from here.

     public static void main(String a[]) { // Main body of programme start from here

         for (int i = 0; i < 6; i++) // for loop start.

              switch (i) { // start body of switch statment

              case 0: // if i=0 than this case is executed.

                  System.out.println("i is zero.");

 

                  break; // this stop the execution and end the program

              case 1: // if i=1 than this case is executed.

                  System.out.println("i is one.");

                  break; // this stop the execution and end the program

              case 2: // if i=2 than this case is executed.

                  System.out.println("i is two.");

                  break; // this stop the execution and end the program

              case 3: // if i=3 than this case is executed.

                  System.out.println("i is three.");

                  break; // this stop the execution and end the program. default:

//if i is greater than t than default statment executed.

              default:

                  System.out.println("i is greater than 3.");

              }

     }

}

outPut:

i is zero.

i is one.

i is two.

i is three.

i is greater than 3.

i is greater than 3.



    

Search
Related Articles

Leave a Comment: