start at 0, count upward by 1's, end at 7 | for ( count = 0; count < 8; count++ ) |
start at 0, count upward by 2's, end at 14 | for ( count = 0; count < 15; count += 2 ) |
start at 1, count upward by 2's, end at 15 | for ( count = 1; count < 16; count += 2 ) |
Here is a program fragment that uses an increment amount contained in a variable.
int count;
int inc;
// ... get inc from the user ...
for ( count = 0; count < 21; count = count + inc )
{
System.out.println( "count is: " + count );
}
System.out.println( "\nDone with the loop.\nCount is now" + count);
(The actual code behind this example does some error checking not seen in the above.)
What is the smallest value of inc
such that the
loop body will execute only one time?
Confirm your answer by testing it with the program.