Download presentation
Presentation is loading. Please wait.
Published byElmer Martin Modified over 9 years ago
1
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements from a number of choices depending on the value of an expression. The statement that will handle precisely this sort of situation is called the switch statement. The choices are called cases. The general form of the switch statement is:
2
The switch StatementtMyn2 switch(expression) { case constant1: statement sequence break; case constant2: statement sequence break; … default: statement sequence }
3
The switch StatementtMyn3 The switch expression must be of type char, byte, short, or int. Frequently, the expression controlling the switch is simply a variable.
4
The switch StatementtMyn4 The selection between a number of cases is determined by the value of an expression that you specify between parentheses following the keyword switch. The case values appear in a case label: case constant: The case constants must be literals of a type compatible with the expression. No two case constants in the same switch can have identical values.
5
The switch StatementtMyn5 The default label identifies the default case, which is a catch-all; the statements that follow are executed if the selection expression does not correspond to any of the case values. The default is optional; if it is not present, no action takes place if all matches fail. The break statement that appears after each set of case statements is absolutely necessary for the logic here.
6
The switch StatementtMyn6 package switchstatement; public class Main { public static void main(String[] args) { for(int i=0; i<10; i++) switch(i) { case 0: System.out.println("i is zero."); break; case 1: System.out.println("i is one."); break; case 2: System.out.println("i is two."); break;
7
The switch StatementtMyn7 case 3: System.out.println("i is three."); break; case 4: System.out.println("i is four."); break; default: System.out.println("i is five or more."); break; } run: i is zero. i is one. i is two. i is three. i is four. i is five or more. BUILD SUCCESSFUL (total time: 5 seconds)
8
The switch StatementtMyn8 The statement list for a case can also be empty, which simply passes control into the statement list for the next case.
9
The switch StatementtMyn9 package switchstatement1; public class Main { public static void main(String[] args) { for(int i=0; i<10; i++) switch(i) { case 0: System.out.println("i is zero."); break; case 1: System.out.println("i is one."); break; case 2: System.out.println("i is two."); break;
10
The switch StatementtMyn10 case 3: case 4: case 5: case 6: System.out.println("i has the value between three and six."); break; default: System.out.println("i is seven or more."); break; } run: i is zero. i is one. i is two. i has the value between three and six. i is seven or more. BUILD SUCCESSFUL (total time: 2 seconds)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.