1 / 25

Program Control using Java - Selection

Department of Computer and Information Science, School of Science, IUPUI. Program Control using Java - Selection. Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu. 4.4   Control Structures (Cont.). Selection Statements if statement Single-selection statement

eburgess
Télécharger la présentation

Program Control using Java - Selection

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. Department of Computer and Information Science,School of Science, IUPUI Program Control using Java - Selection Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

  2. 4.4  Control Structures (Cont.) • Selection Statements • if statement • Single-selection statement • if…else statement • Double-selection statement • switch statement • Multiple-selection statement

  3. 4.5  if Single-Selection Statement • if statements • Execute an action if the specified condition is true • Can be represented by a decision symbol (diamond) in a UML activity diagram • Transition arrows out of a decision symbol have guard conditions • Workflow follows the transition arrow whose guard condition is true

  4. Fig. 4.2 | if single-selection statement UML activity diagram.

  5. 4.6  if…else Double-Selection Statement • if…else statement • Executes one action if the specified condition is true or a different action if the specified condition is false • Conditional Operator ( ?: ) • Java’s only ternary operator (takes three operands) • ?: and its three operands form a conditional expression • Entire conditional expression evaluates to the second operand if the first operand is true • Entire conditional expression evaluates to the third operand if the first operand is false

  6. Fig. 4.3 | if…else double-selection statement UML activity diagram.

  7. Fig. 5.11 | switch multiple-selection statement UML activity diagram with break statements.

  8. Software Engineering Observation 5.2 • Provide a default case in switch statements. Including a default case focuses you on the need to process exceptional conditions.

  9. Outline • GradeBook.java • (1 of 5) • Lines 8-14

  10. Display prompt Outline • GradeBook.java • (2 of 5) • Lines 50-54

  11. switch statement determines which case label to execute, depending on controlling expression Loop condition uses method hasNext to determine whether there is more data to input Outline • GradeBook.java • (3 of 5) • Line 57 • Line 72 controlling expression • Lines 72-94 (grade / 10 ) is controlling expression

  12. default case for grade less than 60 Outline • GradeBook.java • (4 of 5) • Line 91 default case

  13. Outline • GradeBook.java • (5 of 5)

  14. Call GradeBook public methods to count grades Outline • GradeBookTest.java • (1 of 2) • Lines 13-15

  15. Outline • GradeBookTest.java • (2 of 2) • Program output

  16. Portability Tip 5.1 • The keystroke combinations for entering end-of-file are system dependent.

  17. Good Programming Practice 5.8 • Although each case and the default case in a switch can occur in any order, place the default case last. When the default case is listed last, the break for that case is not required. Some programmers include this break for clarity and symmetry with other cases.

  18. switchMultiple-Selection Statement (Cont.) • Expression in each case • Constant integral expression • Combination of integer constants that evaluates to a constant integer value • Character constant • E.g., ‘A’, ‘7’ or ‘$’ • Constant variable • Declared with keyword final

  19. 5.7 break and continue Statements • break/continue • Alter flow of control • break statement • Causes immediate exit from control structure • Used in while, for, do…while or switch statements • continue statement • Skips remaining statements in loop body • Proceeds to next iteration • Used in while, for or do…while statements

  20. Common Programming Error 5.7 • Forgetting a break statement when one is needed in a switch is a logic error.

  21. Exit for statement (break) when count equals 5 Loop 10 times Outline • BreakTest.java • Line 9 • Lines 11-12 • Program output

  22. Skip line 12 and proceed to line 7 when count equals 5 Loop 10 times Outline • ContinueTest.java • Line 7 • Lines 9-10 • Program output

  23. Software Engineering Observation 5.3 • Some programmers feel that break and continue violate structured programming. Since the same effects are achievable with structured programming techniques, these programmers do not use break or continue.

  24. Software Engineering Observation 5.4 • There is a tension between achieving quality software engineering and achieving the best-performing software. Often, one of these goals is achieved at the expense of the other. For all but the most performance-intensive situations, apply the following rule of thumb: First, make your code simple and correct; then make it fast and small, but only if necessary.

  25. Acknowledgements • Deitel, Java How to Program

More Related