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


int count = 0;  
int total = 345; 

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

  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.


What is the true/false value of:

12 > 6 || 18 > 1