public void printRange( int num ) { int count = 0 ; while ( count < num ) { System.out.println( count ) ; count++ ; } }This iterates num times. (Trace out a few examples, just to see). True, we don't know what num is until this method is called, but we do know it just before entering the while loop.
An indefinite loop is a loop that waits for some condition to become true. In general, it's not obvious how many iterations it takes. For example, you might be looking for the first number that is divisible by 2, 3, 5, 7, and 11. You could compute this ahead of time, but it's not easy.
This is considered an indefinite loop, where the number of iterations is unknown ahead of time, or not easily computed.
Here's an example of an indefinite loop.
// finds first number that is divisible // by 2, 3, 5, 7, and 11 public int findIt() { int count = 1 ; while ( ! ( count % 2 == 0 && count % 3 == 0 && count % 5 == 0 && count % 7 == 0 && count % 11 == 0 ) { count++ ; } return count ; }
There's not a compelling reason to use for loops over while loops, except that it organizes indefinite loops more neatly.