Java Reference
In-Depth Information
EXAMPLE 4-20
Consider the following statements (assume that grade is a char variable):
switch (grade)
{
case 'A':
System.out.println("The grade is A.");
break ;
4
case 'B':
System.out.println("The grade is B.");
break ;
case 'C':
System.out.println("The grade is C.");
break ;
case 'D':
System.out.println("The grade is D.");
break ;
case 'F':
System.out.println("The grade is F.");
break ;
default :
System.out.println("The grade is invalid.");
}
In this example, the expression in the switch statement is a variable identifier. The
variable grade is of type char , which is an integral type. The valid values of grade are
'A' , 'B' , 'C' , 'D' , and 'F' . Each case label specifies a different action to take,
depending on the value of grade . If the value of grade is 'A' , the output is:
The grade is A.
EXAMPLE 4-21
The following program illustrates the effect of the break statement. It asks the user to
input a number between 0 and 10 .
 
Search WWH ::




Custom Search