Presentation is loading. Please wait.

Presentation is loading. Please wait.

Part II The Switch Statement

Similar presentations


Presentation on theme: "Part II The Switch Statement"— Presentation transcript:

1 Part II The Switch Statement

2 The switch Statement Provides alternative to if-else chain
For cases that compare value of integer expression to specific value Reserved words: switch case default break

3 The switch Statement (continued)
Syntax: switch (expression) { // start of compound statement case value-1: < terminated with a colon statement1; statement2; break; case value-2: < terminated with a colon statementm; statementn; default: < terminated with a colon statementaa; statementbb; } // end of switch and compound statement

4 The switch Statement (continued)
Once entry point has been located : All further case evaluations are ignored Execution continues through end of compound statement Unless break statement is encountered break statement Causes immediate exit from switch statement

5 import javax.swing.*; public class SelectDiskMaker { public static void main(String[] args) String s1, outMessage; int code; s1 =JOptionPane.showInputDialog("Enter a number:"); code = Integer.parseInt(s1); switch (code)

6 import javax.swing.*; public class SelectDiskMaker { public static void main(String[] args) String s1, outMessage; int code; s1 =JOptionPane.showInputDialog("Enter a number:"); code = Integer.parseInt(s1); switch (code) { case 1: outMessage = "3M Corporation"; break; case 2: outMessage = "Maxell Corporation"; case 3: outMessage = "Sony Corporation"; case 4: outMessage = "Verbatim Corporation"; default: outMessage = "An invalid code was entered"; } // end of switch

7 JOptionPane.showMessageDialog(null, outMessage,
"Program 4.6", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } // end of main() } // end of class

8 Program Design and Development: Introduction to UML
Explicit design Should always be undertaken before coding begins Referred to as program modeling Unified Modeling Language (UML) Program modeling language with its own set of rules and notations Not part of Java language

9 Program Design and Development: Introduction to UML (continued)
Must understand and specify: What objects in system are What can happen to objects When it can happen Each item addressed by number of individual and separate views and diagrams

10 Class and Object Diagrams
Class diagrams Used to describe classes and their relationships Object diagrams Describe specific objects and relationships Attribute Characteristic that each object in class must have Attribute type Primitive Or class type

11 Class and Object Diagrams (continued)
Visibility Defines where attribute can be seen Whether attribute can be used in other classes or is restricted to class defining it Operations Transformations that can be applied to attributes Ultimately coded as Java methods

12 Class and Object Diagrams (continued)
Figure 5.9: A class and object representation

13 Relationships (continued)
Figure 5.17: A multilevel aggregation

14 Relationships (continued)
Figure 5.18: A generalization relationship

15 Common Programming Errors
Assuming if-else statement selecting incorrect choice when problem is values being tested Using nested if statements without including braces to clearly indicate desired structure Inadvertently using assignment operator = in place of relational operator == when comparing Boolean data Forgetting to use break statement to close off case within switch statement

16 Summary Relational expressions if-else statements
Referred to as conditions Used to compare operands if-else statements Used to select between two alternative statements Based on value of relational or logical expression Can contain other if-else statements

17 Summary (continued) Compound statement switch statement
Number of individual statements enclosed within brace pair { and } switch statement Multiway selection statement

18 End of Chapter 5 Be ready to review chapter questions for Thursday
Submit topic for presentation work on today’s lab


Download ppt "Part II The Switch Statement"

Similar presentations


Ads by Google