Java Program using Switch Statement…

Switch statement:

When there are several options and we have to chose only one from the available ones then we can use switch statement.
Syntax:
switch(variable)
{
case value1:
Statement1;
case value2:
Statement2;
Default:
Default statement;
      }
      If non of case match with the value, it will go to default case.
Example:
class Switch
{
  public static void main(String args [])
  {
    int month=4;
    String season;
    switch(month)
    {
      case 12:
      case 1:
      case 2:
      season=”winter”;
      break;
      case 3:
      case 4:
      case 5:
      season=”spring”;
      break;
      case 6:
      case 7:
      case 8:
      season=”summer”;
      break;
      case 9:
      case 10:
      case 11:
      season=”Autumn”;
      break;
      default:
      season=”Bogus month”;
    }
    System.out.println(“April is in the season ” + season + “.”);
  }
}
Output:
D:java >java Switch
April is in the season spring.

Add Comment