1 / 15

Java Switch Statement: Mastering Multiway Branching

Learn how to utilize the switch statement in Java to make decisions based on integral expressions. Discover how to use string expressions in Java 7, along with best practices for implementing switch statements effectively. Explore Enumerations for clearer code readability and restriction of variable values.

mcghie
Download Presentation

Java Switch Statement: Mastering Multiway Branching

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Flow of Control Chapter 3 Part 3 Edited by JJ Shepherd

  2. Switch Statement

  3. The switch Statement • The switch statement is a mutltiway branch that makes a decision based on an integral (integer or character) expression. • Java 7 allows String expressions • In other words you switch on integers, characters, or strings • The switch statement begins with the keyword switchfollowed by an integral expression in parentheses and called the controlling expression.

  4. The switch Statement • A list of cases follows, enclosed in braces. • Each case consists of the keyword case followed by • A constant called the case label • A colon • A list of statements. • The list is searched for a case label matching the controlling expression.

  5. The switch Statement • The action associated with a matching case label is executed. • If no match is found, the case labeled default is executed. • The default case is optional, but recommended, even if it simply prints a message. • Repeated case labels are not allowed.

  6. The switch Statement • Syntax switch (Controlling_Expression) { case Case_Label: Statement(s); break; case Case_Label: … default: … }

  7. The switch Statement • The action for each case typically ends with the word break. • The optionalbreakstatement prevents the consideration of other cases. • The controlling expression can be anything that evaluates to an integral type.

  8. switch to if-else Conversion int a = keyboard.nextInt() switch(a) { case 1: System.out.println(“one”); break; case 2: System.out.println(“two”); break; default System.out.println(“What?”); break; }

  9. switch to if-else Conversion int a = keyboard.nextInt() if(a == 1) { System.out.println(“one”); } else if(a == 2) { System.out.println(“two”); } else { System.out.println(“What?”); }

  10. Enumerations

  11. Enumerations • Consider a need to restrict contents of a variable to certain values • An enumeration lists the values a variable can have • Enumerations are good for using words instead of numbers to represent related values. Makes code more readable. • Exampleenum MovieRating {E, A, B}MovieRating rating;rating = MovieRating.A;

  12. Enumerations • Now possible to use in a switch statement

  13. Enumerations • An even better choice of descriptive identifiers for the constantsenum MovieRating {EXCELLENT, AVERAGE, BAD}rating = MovieRating.AVERAGE;case EXCELLENT: ...

  14. Example!

  15. Summary You have learned about Java branching statements. You have learned about the type boolean.

More Related