Enter an integer: 0 The number 0 is positive Good-bye for now
Here is the program again with some added statements:
import java.io.*; class NumberTester { public static void main (String[] args) throws IOException { BufferedReader stdin = new BufferedReader ( new InputStreamReader( System.in ) ); String inData; int num; System.out.println("Enter an integer:"); inData = stdin.readLine(); num = Integer.parseInt( inData ); // convert inData to int if ( num < 0 ) { System.out.println("The number " + num + " is negative"); // true-branch System.out.println("negative number are less than zero"); // true-branch } else { System.out.println("The number " + num + " is positive"); // false-branch System.out.print ("positive numbers are greater "); // false-branch System.out.println("or equal to zero "); // false-branch } System.out.println("Good-bye for now"); // always executed } }
To include more than one statement in a branch, enclose the statements with braces, { and }. A group of statements grouped together like this is called a block statement, (or usually, just block.) There can be as many statements as you want in a block. A block can go anyplace a single statement can go. All the statements in the true block are executed when the answer to the question is true.
Of course, all the statements in the false block are executed when the answer to the question is false. The false block consists of the block that follows the else. Notice that the very last statement in the program is not part of the false block.