Next: None. Up: Class variables and if statements. Previous: Class variables.


if statement

Textbook: Sections 4.3 and 4.4

Simple if statement

Let's say you have something that you only want to do in some cases. For example: Your program is to flip a coin. If it comes up heads, it should print ``heads''. You don't always want to print ``heads;'' you just want to print it in the situation where you flip heads.

You accomplish this in Java using the if statement.

if(Math.random() < 0.5) IO.println("heads");
This fragment generates a random value between 0.0 and 1.0; if the random value is below 0.5, then it prints ``heads.''

The syntax for if is very similar to the syntax for while. Like the while statement, you can group several statements together using braces.

if(Math.random() < 0.5) {
    IO.println("heads");
    ++heads;
}

else clause

Often you want to do one thing if something happens and another if that thing doesn't happen. Java accomplishes this using the else construct.

if(Math.random() < 0.5) IO.println("heads");
else IO.println("tails");
This will print either ``heads'' or ``tails,'' depending on whether Math.random() returns between 0.0 and 0.5 or between 0.5 and 1.0.

Again, you can use braces to group statements together in the if clause or the else clause.

if(Math.random() < 0.5) {
    IO.println("heads");
    ++heads;
} else IO.println("tails");
Or you can use braces for both clauses.
if(Math.random() < 0.5) {
    IO.println("heads");
    ++heads;
} else {
    IO.println("tails");
    ++tails;
}

So here's a complete program to flip 100 coins and say how many are heads and how many are tails.

import csbsju.cs160.*;

public class FlipMany {
    public static void run() {
        int i = 0;
        int heads = 0;
        int tails = 0;
        while(i < 100) {
            if(Math.random() < 0.5) ++heads;
            else ++tails;
            ++i;
        }
        IO.print(heads);
        IO.println(" heads");
        IO.print(tails);
        IO.println(" tails");
    }
}

Cascaded if statements

Often you have more than two cases to check. The solution is simple: You put an if statement into the else clause.

if(grade >= 90)
    IO.println("A");
else
    if(grade >= 80)
        IO.println("B");
    else
        if(grade >= 70)
            IO.println("C");
        else
            IO.println("bad");
That's the way you do it in Java, and it's called a cascaded if statement.

But a cascaded if statement looks ugly when it's formatted this way: you end up indenting your lines pretty far. Since this situation occurs often, people instead adopt the following formatting convention where all the elses go on the same indentation level.

if(grade >= 90)
    IO.println("A");
else if(grade >= 80)
    IO.println("B");
else if(grade >= 70)
    IO.println("C");
else
    IO.println("bad");
This style is much prettier and more compact.

If each case has several statements to it, you end up with the following.

if(grade >= 90) {
    IO.println("A");
    gpa = 4;
} else if(grade >= 80) {
    IO.println("B");
    gpa = 3;
} else if(grade >= 70) {
    IO.println("C");
    gpa = 2;
} else {
    IO.println("bad");
    gpa = 0;
}

The ambiguous else

Consider the following program.

if(i * i <= n)
    if(n % i == 0)
        IO.print("no");
  else
      IO.print("yes");
This is ambiguous: Does the else go with the first if or the second if?

Java specifies that the else always goes with the innermost if that it can match. So the Java compiler would treat the above just as it would treat the below.

if(i * i <= n) {
    if(n % i == 0)
        IO.print("no");
    else
        IO.print("yes");
}

Quiz 0


Next: None. Up: Class variables and if statements. Previous: if statement.