1// switch case in java example programs
2public class SwitchStatementExample
3{
4 public static void main(String[] args)
5 {
6 char grade = 'A';
7 switch(grade) {
8 case 'A' :
9 System.out.println("Distinction.");
10 break;
11 case 'B' :
12 case 'C' :
13 System.out.println("First class.");
14 break;
15 case 'D' :
16 System.out.println("You have passed.");
17 case 'F' :
18 System.out.println("Fail. Try again.");
19 break;
20 default :
21 System.out.println("Invalid grade");
22 }
23 System.out.println("Grade is: " + grade);
24 }
25}
1// syntax of switch statement in java
2switch(expression)
3{
4 case 1 value :
5 // code goes here
6 break;
7
8 case 2 value :
9 // code goes here
10 break;
11
12 case 3 value :
13 // code goes here
14 break;
15 .
16 .
17 .
18 .
19
20 default: // optional
21 // default code goes here
22}
1Scanner input=new Scanner(System.in);
2int selection = input.nextInt();
3
4while (selection<4)
5{
6 switch(selection){
7 case 1:
8 System.out.println("Please enter amount");
9 double amount=input.nextDouble(); //object of scanner class
10 break;
11
12 case 2:
13 System.out.println("Enter ID number");
14 break;
15
16 case 3:
17 System.out.println("Enter amount to be credited");
18 break;
19 }
20 System.out.println("1. Transfer\n2.Check balance\n3.Recharge");
21 selection = input.nextInt(); // add this
22 }