Page 173 - CA_Blue( J )_Class9
P. 173
6 Scanner sc= new Scanner (System.in);
7 int n1,n2,sum,diff,ch;
8 System.out.print("Enter 1st number : ");
9 n1=sc.nextInt();
10 System.out.print("Enter 2nd number : ");
11 n2=sc.nextInt();
12 System.out.print("Enter 1 for Sum and 2 for Difference: ");
13 ch=sc.nextInt();
14 switch(ch)
15 {
16 case 1: sum = n1 + n2;
17 System.out.println("Sum: "+sum);
18 break;
19 case 2: if(n1>n2)
20 diff = n1 – n2;
21 else
22 diff = n2 – n1;
23 System.out.println("Difference: "+diff);
24 break;
25 default: System.out.println("Wrong Choice ");
26 }
27 }
28 }
You will get the following output:
Fall Through Situation: We know the need for break statement at the end of each case in switch case programs.
But, we can also skip the break statement according to the need of the program. This particular situation is known
as the Fall Through Situation. Let us take a look at an example.
Conditional Constructs in Java 171

