Chapter 3 Flow of Control

Flow of Control Chapter 3 Part 3 Edited by JJ Shepherd JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved 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 switch followed by an integral expression in parentheses and called the controlling expression. JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch

ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved 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. JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved 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. JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved The

switch Statement Syntax switch (Controlling_Expression) { case Case_Label: Statement(s); break; case Case_Label: default:

} JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved The switch Statement The action for each case typically ends with the word break. The optional break statement prevents the consideration of other cases.

The controlling expression can be anything that evaluates to an integral type. JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved 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; } JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved 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?);

} JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved 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.

Example enum MovieRating {E, A, B} MovieRating rating; rating = MovieRating.A; JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Enumerations Now possible to use in a switch statement

JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Enumerations An even better choice of descriptive identifiers for the constants enum MovieRating {EXCELLENT, AVERAGE, BAD} rating = MovieRating.AVERAGE; case EXCELLENT: ...

JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Summary You have learned about Java branching statements. You have learned about the type boolean. JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch

ISBN 0133862119 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Recently Viewed Presentations

  • Characterization - Weebly

    Characterization - Weebly

    For the character that corresponds to your number, provide a detail of him or her using direct characterization. 1 2 3 Indirect characterization Writers most often use indirect characterization to show (not tell) things that reveal the personality of a...
  • The Demographics and Affinities of FaceBook gamers Nick

    The Demographics and Affinities of FaceBook gamers Nick

    People love to play games! Decline in traditional web portals is nota decline in people playing games. People are getting their gaming entertainment through other platforms. Put a device in front of someone and they will want to play a...
  • MarketspaceU - Chapter 2 Enhanced Lecture Slides

    MarketspaceU - Chapter 2 Enhanced Lecture Slides

    Divides the market into different geographical units Country / region / city, city size, density (urban, suburban, rural), ISP domain Demographic Divides the market on the basis of demographic variables Age, gender, income, occupation, education, nationality, family status, Internet connectivity...
  • Chapter 1 Section 3 - PC\|MAC

    Chapter 1 Section 3 - PC\|MAC

    1.1.1 Thinking Spatially Spatial Thinking —thinking about the space on Earth's surface, including where it is located and why it is there. Geographic Patterns-- similarities among different places; example cities located near bodies of water might use the water as...
  • Hazards of Extreme Temperatures - osha.gov

    Hazards of Extreme Temperatures - osha.gov

    It's clear that there are real hazards associated with working in extreme temperatures. These hazards are not just acute hazards - bad things that can happen immediately, but there are also long term health effects. We are going to evaluate...
  • The P.L.A.Y. Project Model Webinars

    The P.L.A.Y. Project Model Webinars

    DIR Model. Six Functional Developmental Levels. Self regulation and shared attention (FDL 1) birth to 3 months . Learn to focus, be calm, and take in the sights and sounds of the world.
  • EL SINTAGMA NOMINAL Y VERBAL - WordPress.com

    EL SINTAGMA NOMINAL Y VERBAL - WordPress.com

    Los verbos transitivos son… aquellos verbos que pueden ir acompañados de un complemento directo. Ejemplos. comprar → Pedro compra dulces. recoger → recoge su cuarto . leer → Lee un libro. mirar → Marta miró el atardecer. saltar → El...
  • Wiley CPAexcel-Efficient Learning

    Wiley CPAexcel-Efficient Learning

    University of Phoenix Portal: The New Classroom * Wiley CPAexcel Welcome Screen- This is the initial view of Wiley CPAexcel that a student will have after clicking the link in the New Classroom for Wiley CPAexcel Prep. If this is...