go to previous page   go to home page   hear noise   go to next page


Enter an integer:
The number 12 is zero or positive
Good-bye for now

The false branch is executed because the answer to the question num < 0 was false.

The Program

Here is the number tester implemented as a program:

import java.util.Scanner;

class NumberTester
  public static void main (String[] args) 
    Scanner scan = new Scanner( System.in );
    int num;

    System.out.println("Enter an integer:");
    num = scan.nextInt();
    if ( num < 0 )                
      System.out.println("The number " + num + " is negative"); 
      System.out.println("The number " + num + " is zero or positive"); 
    System.out.println("Good-bye for now"); 

The words if and else are markers that divide the decision into two sections. The else divides the true branch from the false branch. The if is followed by a question enclosed in parentheses. The expression num < 0 asks if the value in num is less than zero.

Notice that a two-way decision is like picking which of two roads to take to the same destination. The fork in the road is the if statement, and the two roads come together just after the false branch.


The user runs the program and enters -5. What will the program print?