go to previous page   go to home page   hear noise   go to next page
if ( booleanExpression ){
     one or more statements
}else {
     one or more statements }

Answer:

Yes. Logically it is the same as:

if ( booleanExpression )
{
    one or more statements
}
else 
{
    one or more statements 
}

Some programmers use the first style to "save" lines. This was once desirable when computer monitors could display only 20 lines and programmers needed to use a line-saving style to see much of their program. But the style leads to extra errors and extra debugging time, and now monitors can display many more lines.

You still see this style in printed material, because it saves paper, which saves printing expense for the publisher. But with programs displayed on a monitor, lines are free, and the style should be optimized for clarity.

Asking the Right Question

Say that you are shopping at the Mall and find a $44.95 sweater you like, but, due to an impulsive cookie purchase, you might not have enough money. Here is a program that decides if you get the sweater:

import java.util.Scanner;
class SweaterPurchase
{
  public static void main (String[] args)
  { 
    final int price = 4495;    // price in cents

    Scanner scan = new Scanner( System.in );
 
    int cash;                       

    System.out.println("How much do you have, in pennies?");
    cash   = scan.nextInt();     
    
    if (   )
      System.out.println("You can buy the sweater" );
    else
    {
      System.out.println("You can't buy the sweater" );
      System.out.println("You need $" + 
        (price-cash)/100 + "." + (price-cash)%100 + " more." );
    }

  }
}

The reserved word final in the fifth line says that the value held in price will not change during the run of the program. When you use final, the compiler will not let you write a statement that tries to change the value. This is useful for preventing bugs.

QUESTION 7:

What boolean expression should go into the blank? (Assume that there is no sales tax.)