Incremental Java
No Iterations

No Iterations

In the following code, what happens if num is negative?
public void printRange( int num )
{
   int count = 0 ;
   while ( count < num )
   {
      System.out.println( count ) ;
      count++ 
   }
}
Suppose num is -1. Then, when we evaluate count < num, we get 0 < -1. This evaluates to false.

When you write code for a while loop, always think about what kind of values may cause the loop body not to execute, and decide whether it behaves correctly when it doesn't run.