Chapter 5 Control Statements: Part 2; Logical Operators

Chapter 5 Control Statements: Part 2; Logical Operators

Chapter 5 part 1 Control Statements: switch Statement Java How to Program, 10/e Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 1 Introduction switch multiple-selection statement Case study on switch statement

Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 2 switch Multiple-Selection Statement switch multiple-selection statement performs different actions based on the possible values of a constant integral expression of type byte, short, int or char. Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

3 switch Multiple-Selection Statement (Cont.) The switch statement consists of a block that contains a sequence of case labels

and an optional default case. The program evaluates the controlling expression in the parentheses following keyword switch. The program compares the controlling expressions value (which must evaluate to an integral value of type byte, char, short or int, or to a String) with each case label. If a match occurs, the program executes that cases statements. The break statement causes program control to proceed with the first statement after the switch. Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 4

Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 5 switch Multiple-Selection Statement (Cont.)

switch does not provide a mechanism for testing ranges of valuesevery value must be listed in a separate case label. Note that each case can have multiple statements. switch differs from other control statements in that it does not require braces around multiple statements in a case. Without break, the statements for a matching case and subsequent cases execute until a break or the end of the switch is encountered. This is called falling through. If no match occurs between the controlling expressions value and a case label, the default case executes. If no match occurs and there is no default case, program control simply continues

with the first statement after the switch. Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 6 Switch Example Write a program that prompts the user to enter a year and displays the animal for the year. Copyright 1992-2015 by Pearson Education, Inc. All

Rights Reserved. 7 Switch Example Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 8 switch Multiple-Selection Statement (Cont.)

Figure 5.10 shows the UML activity diagram for the general switch statement. Most switch statements use a break in each case to terminate the switch statement after processing the case. The break statement is not required for the switchs last case (or the optional default case, when it appears last), because execution continues with the next statement after the switch.

Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 9 switch Multiple-Selection Statement (Cont.)

When using the switch statement, remember that each case must contain a constant integral expression. An integer constant is simply an integer value. In addition, you can use character constantsspecific characters in single quotes, such as 'A', '7' or '$'which represent the integer values of characters. The expression in each case can also be a constant variablea variable that contains a value which does not change for the entire program. Such a variable is declared with keyword final. Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 10

Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 11 Class AutoPolicy Case Study: Strings in switch Statements can be used as controlling expressions in switch statements, and String literals can be used in case labels.

App requirements: Youve been hired by an auto insurance company that serves these northeast states Connecticut, Maine, Massachusetts, New Hampshire, New Jersey, New York, Pennsylvania, Rhode Island and Vermont. The company would like you to create a program that produces a report indicating for each of their auto insurance policies whether the policy is held in a state with no-fault auto insurance Massachusetts, New Jersey, New York and Pennsylvania. Strings Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 12

Class AutoPolicy Case Study: Strings in switch Statements (Cont.) Class AutoPolicy represents an auto insurance policy. The class contains: int instance variable accountNumber to store the policys account number String instance variable makeAndModel to store the cars make and model (such as a "Toyota Camry")

String instance variable state to store a two-character state abbreviation representing the state in which the policy is held (e.g., "MA" for Massachusetts) a constructor that initializes the classs instance variables Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 13 Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 14

Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 15 Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 16 Class AutoPolicy Case Study: Strings in switch Statements (Cont.)

methods setAccountNumber and getAccountNumber to set and get an AutoPolicys accountNumber instance variable methods setMakeAndModel and getMakeAndModel to set and get an AutoPolicys makeAndModel instance variable methods setState and getState to set and get an AutoPolicys state instance variable method isNoFaultState to return a boolean value indicating whether the policy is held in a nofault auto insurance state; note the method namethe naming convention for a get method that returns a boolean value is to begin the name with "is" rather than "get" (such a method is

commonly called a predicate method). Class AutoPolicyTest (Fig. 5.12) creates two AutoPolicy objects. Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 17 Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 18 Copyright 1992-2015 by Pearson Education, Inc. All

Rights Reserved. 19 Chapter 5: Read In Version 9: 5.6 Or In Version 10: 5.6 and 5.7 Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 20

Recently Viewed Presentations

  • Folie 1 - DKRZ

    Folie 1 - DKRZ

    code_no code_acronym unit description aggregation level----- 130 STP K air_temperature monthly mean 500hPa 130 STP K air_temperature monthly mean 850hPa 167 TEMP2 K air_temperature-at2m monthly mean 2m ...
  • Muscular System - Fairfield-Suisun Unified School District

    Muscular System - Fairfield-Suisun Unified School District

    Irritability - ability to receive and respond to a stimulus. Contractibility - ability to shorten when an adequate stimulus is received. Extensibility - ability to lengthen when an adequate stimulus is received. Elasticity - ability to return to normal shape
  • 4 - Office 365 Fundamentals Deploying

    4 - Office 365 Fundamentals Deploying

    User migration (PST import) or IMAP Migration. New mail file. Deploy Experience - what's added. 6/3/2014. Microsoft Office365. Identity. ... MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION. 6/3/2014 3:45 PM
  • Research Methods - instructor.sdu.edu.kz
  • Intervention for Survivors

    Intervention for Survivors

    A case for Therapeutic Reparative Psychiatry:post-disaster intervention Raquel E. Cohen MD, MPH www.raquelcohendisaster.com * The objectives demand that you have more data and a developing relation to the survivor so that you can identify individual needs and understand the issues...
  • INFOCOM09-Talk

    INFOCOM09-Talk

    Traditional Network. Data plane: forwards packets. Control plane: protocols (spanning tree, OSPF, …) Distributed protocols (switches are peers)
  • Plan for power point presentation for work placement at North ...

    Plan for power point presentation for work placement at North ...

    Simon Slatter 30 years old. ... After many months I met with Dave Witts (head of ICT Department at North Cornwall District Council). Like in any job interview, we talked about how my skills could be of benefit to the...
  • Acids and Bases - Allen Independent School District

    Acids and Bases - Allen Independent School District

    The Chemistry of Acids and Bases Acid and Bases Acids Have a sour taste. Vinegar is a solution of acetic acid. Citrus fruits contain citric acid. ... and place a drop of the solution from the end of the stirring...