Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection Structures: Switch CSC 1401: Introduction to Programming with Java Week 4 – Lecture 1 Wanda M. Kunkle.

Similar presentations


Presentation on theme: "Selection Structures: Switch CSC 1401: Introduction to Programming with Java Week 4 – Lecture 1 Wanda M. Kunkle."— Presentation transcript:

1 Selection Structures: Switch CSC 1401: Introduction to Programming with Java Week 4 – Lecture 1 Wanda M. Kunkle

2 Selection Structures A selection structure is used to choose among alternative courses of action. A selection structure is used to choose among alternative courses of action. So far we have looked at the following types: So far we have looked at the following types: –if –if-else –if-else-if The remaining type is: The remaining type is: –switch

3 Selection Structures The switch selection structure can be used when the course of action to be taken in a program depends upon an integer ( int ) or character ( char ) expression. The switch selection structure can be used when the course of action to be taken in a program depends upon an integer ( int ) or character ( char ) expression.

4 Selection Structures switch switch –Usage: Used in situations where there are multiple alternatives (generally more than two choices) Used in situations where there are multiple alternatives (generally more than two choices) The statements executed depend on which case label the controlling expression matches The statements executed depend on which case label the controlling expression matches –Format: switch (Expression) {// Braces must enclose the case Item list case Item1: Statement(s) break Statement case Item2: Statement(s) break Statement case Item3:... Optional default: Statement(s) } // end switch

5 Selection Structures switch switch –Example: char letter = 'i'; switch (letter) {// Classify a letter as a vowel or a consonant case ‘a’: out.writeln("Vowel"); break; case ‘e’: out.writeln("Vowel"); break; case ‘i’: out.writeln("Vowel"); break; case ‘o’: out.writeln("Vowel"); break; case ‘u’: out.writeln("Vowel"); break; default: out.writeln("Consonant"); } // end switch

6 Selection Structures Let’s look at several different Java programs that classify letters (characters) as vowels or consonants: Let’s look at several different Java programs that classify letters (characters) as vowels or consonants: –Uses if-else-if VowelOrConsonant1.java VowelOrConsonant1.java VowelOrConsonant1.java –Use switch VowelOrConsonant2.java VowelOrConsonant2.java VowelOrConsonant2.java VowelOrConsonant3.java VowelOrConsonant3.java VowelOrConsonant3.java VowelOrConsonant4.java VowelOrConsonant4.java VowelOrConsonant4.java

7 Selection Structures Now let’s look at a Java program that associates integers with days of the week: Now let’s look at a Java program that associates integers with days of the week: –Uses switch DaysOfWeek.java DaysOfWeek.java DaysOfWeek.java

8 Pitfalls of the “switch” Statement Let’s look at the sample programs to consider possible pitfalls … Let’s look at the sample programs to consider possible pitfalls …

9 Topic for Wednesday’s Class File Input and Output (Chapter 6) File Input and Output (Chapter 6)


Download ppt "Selection Structures: Switch CSC 1401: Introduction to Programming with Java Week 4 – Lecture 1 Wanda M. Kunkle."

Similar presentations


Ads by Google