Incremental Java
if statement

Handling Both Cases

The if statement only runs a statement if a condition is true. What if you wanted to run one statement if a condition evaluates to true and another statement if the condition evaluates to false.

This is what the if-else statement does. You can think of the if statement as a special case of the if-else statement.

Example

Let's define a simple method.
public void printIfEven( int num )
{
   System.out.println( "START" ) ;          // LINE 1
   if ( num % 2 == 0 )                      // LINE 2
      System.out.println( "num is even" ) ; // LINE 3
   else
      System.out.println( "num is odd" ) ;  // LINE 4
   System.out.println( "END" ) ;            // LINE 5
}
What happens if you have a method call: obj.printIfEven( 4 )? It prints:
START
num is even
END
Why does it print this? We examine the code above, and trace it. First, 4 is passed to num, the parameter variable, and used to initialize it.

Then, we run LINE 1 which prints "START". Then, we run LINE 2. This evaluates a condition num % 2 == 0, which checks if a number is even or not. If this condition evaluates to true, it runs LINE 3, otherwise it runs LINE 4 and finally runs LINE 5.

Since num is 4, and it's even, it runs LINE 3, which prints "num is even", then skips LINE 4, then runs LINE 5, which prints "END".

num is odd

Suppose the method call is: obj.printIfEven( 3 ).

We still run LINE 1 as usual.

Then, the condition in LINE 2 evaluates to false, so we skip over LINE 3, and run LINE 4. Then we run LINE 5

The output is:

START
num is odd
END

Syntax

The syntax describes the "grammatical" rule for an if-else statement. Here's how it looks:
if ( cond )  // if header
   if body   // if body
else 
   else body  // else body
where cond is a condition (i.e., a Boolean expression).

Both the if body and else body can either be:

Semantics

if ( cond )  // if header
   if body   // if body
else 
   else body  // else body
The semantics are:
If cond evaluates to true, run the if body, else run the else body.
You're guaranteed exactly one of the two bodies will run.

Spot the Error

Look at the following code:
if ( num % 2 == 0 )
   System.out.println( "num is even" ) ;
   System.out.println( "Isn't that great?" ) ;
else
   System.out.println( "num is odd" ) ;
   System.out.println( "Isn't that great?" ) ;
What's wrong with the code? Hint: it doesn't compile.

Recall that an if body (and an else body) can either be a single statement or a block. How many statements are in the if body. Looks like 2. This is how the compiler sees the code:

if ( num % 2 == 0 )
   System.out.println( "num is even" ) ;
System.out.println( "Isn't that great?" ) ;
else // WHAT?? Why is else here?
   System.out.println( "num is odd" ) ;
   System.out.println( "Isn't that great?" ) ;
It sees the if header (i.e., if ( num % 2 == 0 )). So far, so good. Then, it sees the first println(). It says "That's a single statement, so that must be the entire if body". Then, it sees the second println() and says "We're no longer in the if statement, even if it's indented, since I, the compiler, don't care about indentation".

It then sees the else and says "Whoa! You can't have an else, unless the immediate preceding single statement is an if statement, and it's a println(). Error! Error!"

The correct way to write this (assuming the indentation is correct) is:

if ( num % 2 == 0 )
{
   System.out.println( "num is even" ) ;
   System.out.println( "Isn't that great?" ) ;
}
else
{
   System.out.println( "num is odd" ) ;
   System.out.println( "Isn't that great?" ) ;
}
You must add braces to put statements in a block. A single block can be in an if body or else body.

It's code like this that makes some programmers always insist on braces, even if there is a single statement in the body. If you always add braces, you should never run into this problem.