Chapter 5The switch StatementSlide 3Slide 4Slide 5Slide 6Slide 7Slide 8OutlineComparing Data – Very Important!!Comparing Float ValuesSlide 12Comparing CharactersSlide 14Comparing StringsSlide 16Comparing Strings – VIP Example!!Lexicographic OrderingComparing ObjectsSlide 20Repetition StatementsThe while StatementLogic of a while LoopSlide 24Slide 25Slide 26Infinite LoopsSlide 28Nested LoopsNested Loops – Very Important e.g.Questions I like to ask about loopsChapter 5Conditionals and Loops© 2004 Pearson Addison-Wesley. All rights reserved 2/29The 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© 2004 Pearson Addison-Wesley. All rights reserved 3/29The switch Statement•The general syntax of a switch statement is:switch ( expression ){ case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case ...}//end switchswitchandcasearereservedwordsIf expressionmatches value2,control jumpsto here© 2004 Pearson Addison-Wesley. All rights reserved 4/29The switch Statement•Often a break statement is used as the last statement in eacheach case'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© 2004 Pearson Addison-Wesley. All rights reserved 5/29The switch Statementswitch (option){ case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break;}// end switch•An example of a switch statement:Assumes we have a character variable named option that has a character value… as in char option = ‘C’;© 2004 Pearson Addison-Wesley. All rights reserved 6/29The 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© 2004 Pearson Addison-Wesley. All rights reserved 7/29The switch Statement The expression of a switch statement must result in an integral type, meaning an int or a char It cannot be a boolean value, a floating point value (float or double), or another integer type•The implicit boolean condition in a switch statement is equality•You cannot perform relational checks with a switch statement•See GradeReport.java© 2004 Pearson Addison-Wesley. All rights reserved 8/29public class GradeReport{//------------------------------ Again, I omitted lines so I could fit this on one slide. public static void main (String[] args) { int grade, category; Scanner scan = new Scanner (System.in); // you may use a Buffered Reader object as well…. Look these up… System.out.print ("Enter a numeric grade (0 to 100): "); grade = scan.nextInt(); // we use Wrapper classes to convert the String grade to an int grade. category = grade / 10; // What does this do? System.out.print ("That grade is "); switch (category) { case 10: System.out.println ("a perfect score. Well done."); break; case 9: System.out.println ("well above average. Excellent."); break; case 8: System.out.println ("above average. Nice job."); break; case 7: System.out.println ("average."); break; case 6: System.out.println ("below average. You should see the"); System.out.println ("instructor to clarify the material " + "presented in class."); break; default: System.out.println ("not passing."); } // end switch } // end main()} // end class What would you do if I wanted to branch to a specific case and then continue with the remaining cases until the end?© 2004 Pearson Addison-Wesley. All rights reserved 9/29OutlineThe if Statement and ConditionsOther Conditional StatementsComparing DataThe while StatementIteratorsOther Repetition Statements© 2004 Pearson Addison-Wesley. All rights reserved 10/29Comparing Data – Very Important!!•When comparing data using boolean expressions, it's important to understand the nuances of certain data types•Let's examine some key situations:Comparing floating point values for equalityComparing charactersComparing strings (alphabetical order)Comparing object vs. comparing object references© 2004 Pearson Addison-Wesley. All rights reserved 11/29Comparing Float ValuesFloat Values You should rarely use the equality operator (==) when comparing two floating point values (float or double)•Two floating point values are equal only if their underlying binary representations match exactly•Computations often result in slight differences that may be irrelevant•In many situations, you might consider two floating point numbers to be "close enough" even if they aren't exactly equal© 2004 Pearson Addison-Wesley. All rights reserved 12/29Comparing Float Values•To determine the equality of two floats, you may want to use the following technique:if (Math.abs(f1 - f2) < TOLERANCE) System.out.println ("Essentially equal");•If the difference between the two floating point values is less than the tolerance, they are considered to be equal•The tolerance could be set to any appropriate level, such as 0.000001© 2004 Pearson Addison-Wesley. All rights reserved 13/29Comparing CharactersCharacters•As we've discussed, Java character data is based on the Unicode character set•Unicode establishes a particular numeric value for each character, and therefore an ordering•We can use relational operators on character data based on this ordering For example, the character '+' is less than the character 'J' because it comes before it in the Unicode character set One of the appendices provides an overview
View Full Document