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

Answer:

int count = 0;  
int total = 345; 

if ( count > 0 )
{
  if ( total / count > 80 )
    System.out.println("Acceptable Average");
  else
    System.out.println("Low Average");

}
else
  System.out.println("No values to average.");

Use Nested-ifs when Needed

In this code fragment, the "guard" that prevents division by zero is the first if statement. When count is zero, it prevents the true-branch from executing.

It is easy to get caught up in complicated boolean expressions and to use them when a nested-if is more appropriate. The answer to the question is probably better than the original code fragment because it is easier to understand and writes better messages to the monitor.


QUESTION 7:

What is the true/false value of:

12 > 6 || 18 > 1