Switch Statement in Java | Java Switch With Examples

Switch statement in Java: If several options are available then it isn’t recommended to take nested if-else we should go for switch statement so that readability of the code will be improved. “Switch” is a keyword, by using which we can create a selection statement with multiple choices. Multiple choices can be constructed by using a “Case” keyword.

Syntax:

Switch(expression)
{
 case exp1:
statements;
break;
case exp2:
statements;
break;
…………….
…………….
…………….
case expression:
statements;
break;
default:
statements;
}

Explanation: In the above syntax, switch, case, break are keywords. Expr1, expr2 is known as “case labels”. Statements inside case expression need not be closed in braces.  Break statement causes an exit from the switch statement.

 The following rules apply to a switch statement:

1) The expression used in a switch statement must have an integral or enumerated type.
2) You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
3) The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.
 4) When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
5) When the break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
6) Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
7) A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Program:

import java.util.Scanner;
class SwitchExample
{
public static void main(String[] args)
{
 int number;
 Scanner in = new Scanner(System.in);
 System.out.println("Enter a number");
 number = in.nextInt();
 switch(number)
 {
  case 1:
  System.out.println("Welcome to TutorialWeb.org");
  break;
  case 2:
  System.out.println("I am Java programmer");
  break;
  case 3:
  System.out.println("Java programming in TutorialWeb.org");
  break;
  default:
  System.out.println("Switch Program Example");
  break;
 }
}
}

Output:

Enter a number
1
Welcome to TutorialWeb.org

Explanation:

1) The integer value “1” is assigned to a number variable.
2) After switch case decides then 1st case block of code gets executed.
3) Switch case executes code from top to bottom. It will validate case number with the variable number.
4) If no match is found then it will jump to the next case. It checks until found if the case is not found, the default will be executed.

Program:

import java.util.Scanner;
class SwitchMonthExample
{
public static void main(String[] args)
{
 int number;
 Scanner in = new Scanner(System.in);
 System.out.println("Enter a number");
 number = in.nextInt();
 switch(number)
 {
  case 1:
  System.out.println("January");
  break;
  case 2:
  System.out.println("February");
  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("August");
  break;
  case 9:
  System.out.println("September");
  break;
  case 10:
  System.out.println("October");
  break;
  case 11:
  System.out.println("November");
  break;
  case 12:
  System.out.println("December");
  break;
  default:
  System.out.println("Enter number between 1 to 12");
  break;
 }
}
}

Output:

Enter a number
7
July
Share:

Ads

Search This Blog

  • ()
Powered by Blogger.

Strings in C With Syntax, C String Program with output

Strings in C :  Strings can be defined as the one-dimensional array of characters terminated by a null ('\0'). The difference betwee...