Page 176 - CA_Blue( J )_Class9
P. 176
8.2 DIFFERENCE BETWEEN IF STATEMENT AND SWITCH STATEMENT
if statement switch statement
The flow of control is bidirectional for a single The flow of control is multidirectional depending
comparison. on the choice
All kinds of the relational operators are used for Checking is satisfied if the choice variable is
checking. matching the case value.
Any data type can be used for checking. Only limited data types can be used for checking
(e.g., int, char, String, enum), float, double,
long, and boolean cannot be used.
More efficient in case a value is to be tested Switch can test only for equality, so for the rest
against a set of constants. of comparisons one needs to use if-else.
Program 11 Write a program using switch case to input the grade and print the message.
Grade Message
A Very Good
B Good
C Passed
F Try Harder Next time
1 class grades
2 {
3 public static void main(char c)
4 {
5 System.out.println("Your grade is " + c);
6 switch(c)
7 {
8 case 'A' :
9 System.out.println("Very Good");
10 break;
11 case 'B' :
12 System.out.println("Good");
13 break;
14 case 'C' :
15 System.out.println("Passed");
16 case 'F' :
17 System.out.println("Try Harder Next time");
18 break;
19 default :
20 System.out.println("Invalid grade");
21 }
174 Touchpad Computer Applications-IX

