Slide 1

Slide 1 text

CSE110 Principles of Programming with Java Lecture 10: switch Statement and Operator ? Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com Office Hours: By appointment

Slide 2

Slide 2 text

switch Statement

Slide 3

Slide 3 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 3 The switch Statement • The switch statement provides another means to decide which statement to execute next • The switch statement evaluates an expression, then attempts to match the result to one of several possible cases • Each case contains a value and a list of statements • The flow of control transfers to statement associated with the first value that matches

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 4 The switch Statement • The general syntax of a switch statement is

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 5 Example 1A String s = JOptionPane.showInputDialog(“Write a number”): int option = Integer.parseInt(s); switch (option) { case 1: System.out.println(“Phoenix”); break; case 2: System.out.println(“NY”); break; case 3: System.out.println(“Seattle”); break; default: System.out.println(“Error”); break; } System.out.println(“Bye.”);

Slide 6

Slide 6 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 6 Example 1B String s = JOptionPane.showInputDialog(“Write a number”): int option = Integer.parseInt(s); switch (option) { case 1: System.out.println(“Phoenix”); break; case 2: System.out.println(“NY”); break; case 3: System.out.println(“Seattle”); break; } System.out.println(“Bye.”);

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 7 Example 1C String s = JOptionPane.showInputDialog(“Write a number”): int option = Integer.parseInt(s); switch (option) { case 1: System.out.println(“Phoenix”); case 2: System.out.println(“NY”); case 3: System.out.println(“Seattle”); default: System.out.println(“Error”); } System.out.println(“Bye.”);

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 8 Example 1D String s = JOptionPane.showInputDialog(“Write a number”): int option = Integer.parseInt(s); switch (option) { case 1: System.out.println(“Phoenix”); break; case 2: System.out.println(“NY”); break; case 3: System.out.println(“Seattle”); break; default: System.out.println(“Error”); } System.out.println(“Bye.”);

Slide 9

Slide 9 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 9 The switch Statement • The expression of a switch statement must result in an int or a char • It cannot be a boolean value, a floating-point value (float or double) • A switch statement tries to match the expression with a value • You cannot perform relational checks with a switch statement

Slide 10

Slide 10 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 10 break • Often a break statement is used as the last statement in each case's statement list • A break statement causes control to transfer to the end of the switch statement • If a break statement is not used, the flow of control will continue into the next case

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 11 default • A switch statement can have an optional default case • The default case has no associated value and simply uses the reserved word default • If the default case is present, control will transfer to it if no other case value matches • Though the default case can be positioned anywhere in the switch, usually it is placed at the end • If there is no default case, and no other value matches, control falls through to the statement after the switch

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 12 Example 2 | using char String s = JOptionPane.showInputDialog(“Write a letter”): char letter = s.charAt(0); switch (letter) { case 'A': System.out.println("It is an A"); break; case 'B': System.out.println("It is a B"); break; case 'C': System.out.println("It is a C"); break; default: System.out.println("Invalid option"); }

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 13 Example 3 String s = JOptionPane.showInputDialog(“Write your grade”); int grade = Integer.parseInt(s); switch (grade) { case 100: System.out.println(“A+”); break; case 95: case 90: System.out.println(“A”); break; case 85: case 80: System.out.println(“B”); break; default: System.out.println(“C”); } System.out.println(“Bye.”); // What is the result for grade = 97 ?

Slide 14

Slide 14 text

The operator ?

Slide 15

Slide 15 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 15 The Conditional Operator • Java has a conditional operator that evaluates a boolean condition that determines which of two other expressions is evaluated • The result of the chosen expression is the result of the entire conditional operator • Its syntax is: condition ? expression1 : expression2 • If the condition is true, expression1 is evaluated; if it is false, expression2 is evaluated

Slide 16

Slide 16 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 16 The Conditional Operator • The conditional operator is similar to an if-else statement, except that it forms an expression that returns a value • For example: larger = ((num1 > num2) ? num1 : num2); • If num1 is greater that num2, then num1 is assigned to larger; otherwise,num2 is assigned to larger • The conditional operator is ternary because it requires three operands

Slide 17

Slide 17 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 17 Example System.out.println ("Your change is " + count + ((count == 1) ? "Dime" : "Dimes")); • If count equals 1, then "Dime" is printed • If count is anything other than 1, then "Dimes" is printed

Slide 18

Slide 18 text

CSE110 - Principles of Programming Javier Gonzalez-Sanchez [email protected] Summer 2020 Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.