【正文】
Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition 2 More Conditionals and Loops ? Now we can fill in some additional details regarding Java conditional and repetition statements ? Chapter 6 focuses on: – the switch statement – the conditional operator – the do loop – the for loop – drawing with the aid of conditionals and loops – dialog boxes 3 Outline The switch Statement The Conditional Operator The do Statement The for Statement Drawing with Loops and Conditionals Dialog Boxes 4 The switch Statement ? The switch statement provides another way to decide which statement to execute next ? The switch statement evaluates an expression, then attempts to match the result to one of several possible cases ? Each case contains a value and a list of statements ? The flow of control transfers to statement associated with the first case value that matches 5 The switch Statement ? The general syntax of a switch statement is: switch ( expression ) { case value1 : statementlist1 case value2 : statementlist2 case value3 : statementlist3 case ... } switch and case are reserved words If expression matches value2, control jumps to here 6 The switch Statement ? Often a break statement is used as the last statement in each case39。s statement list ? A break statement causes control to transfer to the end of the switch statement ? If a break statement is not used, the flow of control will continue into the next case ? Sometimes this may be appropriate, but often we want to execute only the statements associated with one case 7 The switch Statement switch (option) { case 39。A39。: aCount++。 break。 case 39。B39。: bCount++。 break。 case 39。C39。: cCount++。 break。 } ? An example of a switch statement: 8 The switch Statement ? A switch statement can have an optional default case ? The default case has no associated value and simply uses the reserved word default ? If the default case is present, control will transfer to it if no other case value matches ? If there is no default case, and no other value matches, control falls through to the statement after the switch 9 The switch Statement ? The type of a switch expression must be integers, characters, or enumerated types ? As of Java 7, a switch can also be used with strings ? You cannot use a switch with floating point values ? The implicit boolean condition in a switch statement is equality ? You cannot perform relational checks with a switch statement ? See 10 //******************************************************************** // Author: Lewis/Loftus // // Demonstrates the use of a switch statement. //******************************************************************** import 。 public class GradeReport { // // Reads a grade from the user and prints ments accordingly. // public static void main (String[] args) { int grade, category。 Scanner scan = new Scanner ()。 (Enter a numeric grade (0 to 100): )。 grade = ()。 category = grade / 10。 (That grade is )。 continue 11 continue switch (category) { case 10: (a perfect score. Well done.)。 break。 case 9: (well above average. Excellent.)。 break。 case 8: (above average. Nice job.)。 break。 case 7: (average.)。 break。 case 6: (below average. You should see the)。 (instructor to clarify the material + presented in class.)。 break。 default: (not passing.)。 } } } 12 continue switch (category) { case 10: (a perfect score. Well done.)。 break。 case 9: (well above average. Excellent.)。 break。 case 8: (above average. Nice job.)。 break。 case 7: (average.)。 break。 case 6: (be