DOC PREVIEW
UNF COP 2551 - Lecture Notes

This preview shows page 1-2-3-27-28-29 out of 29 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 25Infinite LoopsSlide 27Nested LoopsNested Loops – Very Important e.g.Chapter 5Conditionals and Loops© 2004 Pearson Addison-Wesley. All rights reserved 5-2The 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 5-3The 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 ...}switchandcasearereservedwordsIf expressionmatches value2,control jumpsto here© 2004 Pearson Addison-Wesley. All rights reserved 5-4The 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-5The switch Statementswitch (option){ case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break;}•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 5-6The 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 5-7The 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 (page 225)© 2004 Pearson Addison-Wesley. All rights reserved 5-8public 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); // we use buffered reader…. 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© 2004 Pearson Addison-Wesley. All rights reserved 5-9OutlineThe if Statement and ConditionsOther Conditional StatementsComparing DataThe while StatementIteratorsOther Repetition StatementsDecisions and GraphicsMore Components© 2004 Pearson Addison-Wesley. All rights reserved 5-10Comparing 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 equalityComparing charactersComparing strings (alphabetical order)Comparing object vs. comparing object references© 2004 Pearson Addison-Wesley. All rights reserved 5-11Comparing 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 5-12Comparing 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 5-13Comparing 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 Appendix C provides an overview of Unicode Look at this Appendix!!© 2004 Pearson Addison-Wesley. All rights reserved 5-14Comparing Characters•In Unicode, the digit characters (0-9) are contiguous and in order•Likewise, the uppercase letters (A-Z) and


View Full Document

UNF COP 2551 - Lecture Notes

Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?