Page 187 - Computer science 868 Class 12
P. 187
For example:
switch(ch)
{
case 1: System.out.println("1");
case 2: System.out.println("11");
case 3: System.out.println("111");
default: System.out.println("-----");
}
Input:
1
Output:
1
11
111
-----
Nested switch case
When a switch statement is present inside another switch statement, then it is known as a nested switch case. It is
more efficient than if else statements and performs execution faster.
For example:
Input the grades of a student scored in a degree course and the year of passing the course. Print the grades and the
year in words.
class gradeandyear
{
public static void main(int yr, int mk)
{
switch(yr) //Switch expression
{
//Case statements
case 1: System.out.println("First year student");
break;
case 2: System.out.println("Second year student");
case 3: switch(mk)
{
case 70: System.out.println("You are not eligible for certificate");
break;
case 80: System.out.println("You are eligible for certificate");
break;
default: System.out.println("Wrong marks");
}
break;
//Default case statement
default: System.out.println("Wrong Year");
}
}
}
185
Statements and Scope 185

