How to use the switch statement in java?

Total Post:16

Points:112

 25  View(s)
Ratings:
Rate this:

Switch Statement in Java;-

  • A switch statement is executed one statement for a different condition.
  • In switch expression one or N number of cases values.
  • The java switch expression is int, long, byte, sort and string.
  • If the duplicate value arises, shows compile time error.

Syntax-

switch(expression)

{
case a:
  //statement
break;
case b:
//statement
break;
.
.
.
.
default:
//statement
}

Example-

class Student
{
public static void main(String args[])
{
int month=10;
switch (month)
{
case 1:
System.out.println(“Jan”);
break;
case 2:
System.out.println(“Feb”);
break;
case 3:
System.out.println(“March”);
break;
case 4:
System.out.println(“April”);
break;
case 5:
System.out.println(“May”);
break;
case 6:
System.out.println(“June”);
break;
case 7:
System.out.println(“July”);
break;
case 8:
System.out.println(“Aug”);
break;
case 9:
System.out.println(“Sept”);
break;
case 10:
System.out.println(“Oct”);
break;
case 11:
System.out.println(“Novem”);
break;
case 12:
System.out.println(“Decem”);
break;
default:
System.out.println(“data not found”);
}
}
}

Output-
Oct


Answer

Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.