Page 151 - computer science (868) class 11
P. 151
case …..
.
.
default: statements;
}
Flowchart:
Switch
Conditional expression
True
Case Statement 1
Condition 1 break;
False
True
Case Statement 2
Condition 2 break;
False
False
True
Case Statement n
Condition n break
False
True
Default Default
Statement
Statement just
below switch case
For example,
1. //The following program snippet uses numeric data type
int choice=sc.nextInt();
switch(choice)
{
case 1: System.out.println("Good Morning"):
break;
case 2: System.out.println("Good Afternoon");
break;
case 3: System.out.println("Good Evening");
break;
default: System.out.println("Good Night");
}
Here, the variable choice is compared with the value of each case statement. Depending on the values 1 to 3, the
corresponding statement will be printed. If the value entered is other than 1 to 3, the message “Good Night” will
be displayed.
2. // The following program snippet uses character data type
int a=6, b=3;
char ch=sc.next().charAt(0);
switch(ch)
{
149
Statements and Scope 149

