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

CSE110 Lecture 09

CSE110 Lecture 09

Principles of Programming with Java
Nested if-else Statements
(202005)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez

May 30, 2017
Tweet

Transcript

  1. CSE110 Principles of Programming with Java Lecture 09: Nested if-else

    Statements Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com Office Hours: By appointment
  2. For the Midterm Exam

  3. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 3 Proctoring

    https://youtu.be/XuX8WoeAycs
  4. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 4 Proctoring

  5. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 5 Proctoring

  6. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 6 Proctoring

  7. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 7 Agenda

    • Sample Exam will be available during labs to Test your System • Midterm Exam available ONLY June 8 from 6:00 pm to 7:15 pm Important: a) Show your environment. You need a Camera b) It is a CLOSED BOOK exam. Do not use any material c) Scratch paper is allowed. Empty White Paper. Show it on camera when you show your environment!
  8. if-else

  9. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 9 if-else

    statement An else clause can be added to an if statement to make an if-else statement if ( condition ) { statement1; } else { statement2; } • If the condition is true, statement1 is executed; if the condition is false, statement2 is executed • One or the other will be executed, but not both
  10. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 10 Flow

    chart of an if-else statement
  11. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 11 Nested

    if Statements • The statement executed as a result of an if statement or else clause could be another if statement • These are called nested if statements • An else clause is matched to the last unmatched if (no matter what the indentation implies) • Braces can be used to specify the if statement to which an else clause belongs
  12. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 12 if-else

    if-else if (temp > 100) { System.out.println("It is hot!"); } else { if (temp > 80) { System.out.println("It is warm"); } else { if (temp > 50){ System.out.println("It is chilly"); } else { System.out.println("It is cold!"); } } }
  13. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 13 Block

    Statements • Several statements are grouped together into a block statement • A block is delimited by braces: {...} • For example, in an if-else statement, the if portion, or the else portion, or both, could be block statements • There is no need to use braces if there is only one statement or one set of “if-else” within the outer “if” statement
  14. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 14 Block

    Statements
  15. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 15 if-else

    if-else if (temp > 100) { System.out.println("It is hot!"); } else { if (temp > 80) { System.out.println("It is warm"); } else { if (temp > 50){ System.out.println("It is chilly"); } else { System.out.println("It is cold!"); } } }
  16. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 16 if-else

    if-else This is the same the the previous one: if (temp > 100) { System.out.println("It is hot!"); } else if (temp > 80) { System.out.println("It is warm"); } else if (temp > 50) { System.out.println("It is chilly"); } else { System.out.println("It is cold!"); }
  17. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 17 Logical

    NOT • The logical NOT operation is also called logical negation or logical complement • If some boolean condition a is true, then !a is false; if a is false, then !a is true • Logical expressions can be shown using truth tables
  18. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 18 Logical

    AND and Logical OR • Since && and || each have two operands, there are four possible combinations of conditions a and b
  19. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 19 Logical

    Operators Conditions can use logical operators to form complex expressions if (total < MAX+5 && !found) System.out.println ("Processing..."); Logical operators have precedence relationships among themselves and with other operators • The relational or arithmetic operators have higher precedence than logical AND and logical OR • logical NOT has higher precedence than logical AND. Logical AND has higher precedence than logical OR
  20. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 20 Example

    int examGrade = 90; int assignmentGrade = 80; int quizGrade = 85; if (examGrade > 85 && assignmentGrade > 85) System.out.println(“Well done!”); else if (quizGrade < 70 || assignmentGrade < 85) System.out.println(“Houston, we have a problem”);
  21. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 21 Example

  22. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 22 Example

  23. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 23 Reference

    Textbook – Section 3.1, 3.2, and 3.3
  24. CSE110 - Principles of Programming Javier Gonzalez-Sanchez javiergs@asu.edu 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.