Example For Switch Statement In Java

The switch is a control statement using as an alternative to if else if. In this post, we will show you one example for switch statement in Java programming.

Example For Switch Statement

Syntax: switch (expression)

  {   case value_1:
          statement 1;
          break;
      case value_2:
          statement 2;
          break;
          ......
      case value_n:
          statement n;
          break;
      default:
      default statement;
   }

Write a program to print the words of all numbers from 0 to 9.

class Eg
{    public static void main(String args [])
     {    for(int i=0;i<10; i++)
         {    switch(i)
             {    case 0; System.out.printIn("zero");
                       break;
                  case 1; System.out.printIn("one");
                       break;
                  case 2; System.out.printIn("two");
                       break;
                  case 3; System.out.printIn("three");
                       break;
                  case 4; System.out.printIn("four");
                       break;
                  case 5; System.out.printIn("five");
                       break;
                  case 6; System.out.printIn("six");
                       break;
                  case 7; System.out.printIn("seven");
                       break;
                  case 8; System.out.printIn("eight");
                       break;
                  case 9; System.out.printIn("nine");
                       break;
              }
          }
     }
}

The above program will print words of numbers from 0 to 9.

Why This Example For Switch Statement?

For better understanding. Hope you will learn deeply about the specific statement.

We hope you have enjoyed the post and found it useful. If you did, please consider sharing our article with your friends on social media. It will help us to write more related posts.

Leave a Reply