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

CSE110 Lecture 08

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

CSE110 Lecture 08

Principles of Programming with Java
Conditional Statements
(202005)

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. CSE110 Principles of Programming with Java Lecture 08: Conditional Statements

    Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 2 Announcement

    Assignment 1 is due today (11:59 PM AZ Time) Midterm Exam (Monday June 8, 6:00PM to 7:15PM AZ time)
  3. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 5 Input

    import javax.swing.JOptionPane; public class Lecture06 { public static void main(String [] args) { String test1= JOptionPane.showInputDialog(”write here:"); int value1 = Integer.parseInt(test1); double value2 = Double.parseDouble(test1); System.out.println(value1); System.out.println(value2); } }
  4. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 6 Expressions

    class global variables methods statements local variables instructions expressions arithmetic expression relational expression logical expression
  5. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 7 Relational

    expressions A condition uses relational operators, which all return boolean results: == equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to Note the difference between the equality operator (==) and the assignment operator (=)
  6. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 8 Boolean

    (logical) expressions A condition uses relational operators, which all return boolean results: && AND || OR ! NOT
  7. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 10 Topics

    class global variables methods statements instructions local variables expressions
  8. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 11 Topics

    class global variables methods statements instructions local variables expressions conditional Statements loop Statements
  9. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 12 Flow

    of Control Unless specified otherwise, the order of statement execution through a method is linear: one statement after the other in sequence (top down order). public static void main (String [] args) { System.out.println(“one”); System.out.println(“two”); System.out.println(“three”); } The order of statement execution is called the flow of control
  10. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 13 Flow

    of Control Some programming statements modify that order (flow of control), allowing us to: • decide whether to execute a statement, or • perform a statement over and over, repetitively These decisions are based on a boolean expression (also called a condition) that evaluates to true or false.
  11. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 15 Conditional

    Statements • A conditional statement lets us choose which statement will be executed next • Java's conditional statements are o the if statement o the if-else statement o the switch statement o the operator ?
  12. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 16 if

    statement • The if statement has the following syntax:
  13. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 17 Example

    int MAX = 5; int sum = 30; int delta = 0; if (sum > MAX) { delta = sum - MAX; } System.out.println("The delta is " + delta);
  14. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 19 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
  15. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 21 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
  16. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 22 if-else

    if-else You can also have multiple conditions to be verified: 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 | 23 if-else

    if-else The code from the previous page is equivalent to: 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!"); } } }
  18. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 24 Reference

    Textbook – Section 3.1, 3.2, and 3.3
  19. 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.