Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE110 Lecture 10

CSE110 Lecture 10

Principles of Programming with Java
switch Statement and Operator ?
(202005)

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 4 The

    switch Statement • The general syntax of a switch statement is
  4. 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.”);
  5. 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.”);
  6. 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.”);
  7. 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.”);
  8. 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
  9. 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
  10. 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
  11. 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"); }
  12. 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 ?
  13. 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
  14. 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
  15. 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
  16. 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.