Nested If Statements Worksheet

What is the output of each of the following statements? Assume that a = 10, b = 5, c = 8, and temp = 0

1.  if (a <= b)
       b = a;
    System.out.println(a + "  " + b);

2.  if (a <= b) {
       b = a;
       System.out.println(a + "  " + b);
    }

3.  if (a < b)
       if (b + c < a)
          System.out.println(c + "  " + b);
       else
          System.out.println(b + "  " + c);
    System.out.println(b + "  " + a);

4.  if (a > b)
       if (b + c < a)
          System.out.println(c + "  " + a);
       else
          System.out.println(b + "  " + c);
    else
       System.out.println(b + "  " + a);

5.  if (a > b) {
       c = c + b;
       if (c > a)
          System.out.println(temp + "  " + c);
       else {
          b = 2 * b;
          System.out.println(b + "  " + c);
       }
    }
    else {
       a += 5;
       System.out.println(a + "  " + b);
    }

6.  if (a < c)
       a += c;
    else if (b > temp)
       a -= c;
    else
       a *= 2;
    System.out.println(a + "  " + b + "  " + c);

7.  if (a < c)
       b = a + c;
    else {
       c = a + b;
       if (b > temp)
          a = b + c;
    }
    System.out.println(a + "  " + b + "  " + c);

8.  if (a - b >= c) {
       temp += 1;
    else if (b >= temp + 2)
       temp -= 1;
    else
       temp += 10;
    System.out.println(temp);

Write the Java statements for each of the following:

9.  Read a person's age and print a message stating whether or not the
person is old enough to drink. The minimum age to drink is 21.

10. Read a person's age and print a message stating whether the person
is too young to drive, old enough to drive but too young to drink, or old
enough to drive and drink (but not at the same time!). The minimum age to
drive is 17.

11. Read the temperature and print one of the following messages:
             Temperature          Message
             more than 83         hot
             68 to 83             ideal
             45 to 67             cool
             less than 45         cold

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2013