$30 off During Our Annual Pro Sale. View Details »

CSE110 Lecture 08

CSE110 Lecture 08

Principles of Programming with Java
Conditional Statements
(202005)

Javier Gonzalez-Sanchez
PRO

May 26, 2017
Tweet

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

    View Slide

  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)

    View Slide

  3. Previously

    View Slide

  4. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 4
    Input

    View Slide

  5. 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);
    }
    }

    View Slide

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

    View Slide

  7. 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 (=)

    View Slide

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

    View Slide

  9. Control Statements

    View Slide

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

    View Slide

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

    View Slide

  12. 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

    View Slide

  13. 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.

    View Slide

  14. Conditional Statements

    View Slide

  15. 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 ?

    View Slide

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

    View Slide

  17. 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);

    View Slide

  18. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 18
    Flow Chart of an if statement

    View Slide

  19. 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

    View Slide

  20. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 20
    Flow chart of an if-else statement

    View Slide

  21. 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

    View Slide

  22. 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!");
    }

    View Slide

  23. 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!");
    }
    }
    }

    View Slide

  24. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 24
    Reference
    Textbook – Section 3.1, 3.2, and 3.3

    View Slide

  25. 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.

    View Slide