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

Answer:

if ( num < 0 )
  System.out.println("The number " + num + " is negative.");         // true-branch
else
{
  System.out.println("The number " + num + " is zero or 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

The true branch has one statement. The false branch has one statement, a block containing three statements.


Practice

At a movie theater box office a person less than age 13 is charged the "child rate". Otherwise a person is charged the "adult rate." Here is a partially complete program that does this:


Movie Tickets
import java.util.Scanner;
public class BoxOffice
{
  public static void main (String[] args) 
  {
    Scanner scan = new Scanner( System.in );
    int age;
 
    System.out.println("Enter your age:");
    age = scan.nextInt();

    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 contains only one statement, but this is OK. Often programmers do this for clarity.


QUESTION 11:

Fill in the blank so that a person under the age of 13 is charged the child rate.


go to previous page   go to home page   go to next page