CS 1110/1111: Introduction to Programming

Lecture 9

Announcements

Talking Points

Thus far, all statements have been of the form do one thing, like x = x * 3 - 1; or System.out.println("print something");

Sometimes we only sometimes want to do something, or sometimes one thing sometimes another.

If Statement

We can put if (some condition) in front of any statement.

if (x < 3) x = x + 1;

Usually we put if in front of a compound statement or block, which is a list of statements surrounded by curly braces.

if (x < 3) {
    x = x + 1;
}
System.out.println("This line always happens, not matter what x was");
if (x > 7) {
    System.out.println("x is too big!");
    x = 7;
}

If the thing inside the parentheses is true, we do the next statement or block. If not, we skip it.

If-else Statement

The statement after an if statement can be an else statement. It will only happen if the if part didn't.

System.out.println("Let's look at x = " + x);
if (x < 3) {
    System.out.println("x less than 3");
} else {
    System.out.println("x is 3 or bigger");
}
System.out.println("Done looking at x = " + x);

Nesting Statements

You can put any statement inside an if or else, including other if and/or if-else statements.

if (x < 3) {
    System.out.println("x less than 3");
    if (x == 0) { 
        System.out.println("in fact, it is zero");
    }
} else {
    System.out.println("x is 3 or bigger");
}

Comparison and Logical Operators

We can compare primitive values with <, >, <= (which means ≤), >= (which means ≥), == (which means =), or != (which means ≠)

We can combine boolean values with && (which means and) or || (which means or); we can also swap true to false and vice-versa with ! (which is pronounced bang and means not)

If we need to compare objects (like Strings, for example), we should use the oneObject.equals(otherObject), not ==.

if (name.equals("Thomas Jefferson") && (year == 1743 && month >= 4) || (year == 1744 && month < 4)) {
    System.out.println("You were born less than a year ago!") 
}

Code from Class

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    
    System.out.print("Type an integer: ");
    int n = s.nextInt();
    
    if (n == 0) {
        System.out.println(n + " is neither positive nor negative");
    }
    if (n > 0) {
        System.out.println(n + " is a positive number");
        System.out.println("We like "+n);
        if (n == 42) {
            System.out.println("42 is the answer");
        }
    }
    else {
        System.out.println(n + " is a negative number");
        if (n < -100) {
            System.out.println("... REALLY negative");
        }
        else {
            System.out.println("... but not very negative");
        }
    }
    // < > == <= >= !=
    System.out.println(n + " was the number");
}
public static void main(String[] args) {
    int x = 300;
    
    //if (0 < x < 10) {
    if (0 < x && x < 10) { // "&&" means "and"
        System.out.println("x is a single-digit number");
    }

    if (!(x > 0) || (x > 100)) { // "||" means "or"
        System.out.println("x is not a number we teach in kindergarten");
    }

    // 0 < x < 10
    // (0 < x) < 10
    // (true) < 10
    
    // !(x > 0) || (x > 100)
    // !(-3 > 0) || (-3 > 100)
    // !(false) || (false)
    // true || false
    // true
    
    String s = "yes";
    String t = "no";
    if (s.equals(t)) {
        System.out.println(s+" and "+t+" are the same");
    } else {
        System.out.println(s+" and "+t+" are not the same");
    }
}
public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.print("Are you a SEAS student: ");
    String seas = s.next();
    if (seas.equals("yes")) {
        System.out.print("Do you want to minor in CS? ");
        String minor = s.next();
        if (minor.equals("yes")) {
            // TODO: finish this
        } else {
            System.out.print("What year are you? ");
            int year = s.nextInt();
            if (year == 1) {
                System.out.println("Use the normal declaration form.");
            } else {
                System.out.println("Go to the CS office before 17 Feb.");
            }
        }
    } else {
        System.out.print("Do you want to transfer to SEAS? ");
        String transfer = s.next();
        // TODO: finish this
    }
}
Copyright © 2014 by Luther Tychonievich. All rights reserved.