if ( num < 0 ) System.out.println("The number " + num + " is negative"); // 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
Notice that in this program that the true branch has one statement, which is not a block, and that the false branch has one statement, which is a block (and as a block contains three statements.)
At a movie theater box office a person less than age 17 is charged the "child rate". Otherwise a person is charged "adult rate." Here is a partially complete program that does this:
import java.io.*; class BoxOffice { public static void main (String[] args) throws IOException { BufferedReader stdin = new BufferedReader ( new InputStreamReader( System.in ) ); String inData; int age; System.out.println("Enter your age:"); inData = stdin.readLine(); age = Integer.parseInt( inData ); // convert inData to int if ( __________________ ) { System.out.println("Child rate."); } else { System.out.println("Adult rate."); } System.out.println("Enjoy the show."); // always executed } }
In this program, the true branch and the false branch are both blocks. Each block has only one statement inside of it, but this is OK. All you need to do is complete the blank so the program picks the correct block for the age that was input.