Your statement is incorrect : you DO have the exact number of the stopping point because it was entered by the user. This is asking for an implementation as a for loop because those are the three parameters it uses. A for loop looks like this :
for( Initialize; Condition; Increment )
{
}
Following is a little sample code that implements a for loop and each of the three parameters is a call to a function. In most loops they are simple statements and the first line of each of the three functions is what the statement usually looks like. Anyway, this is meant as a mini-tutorial. I recommend that you read up further on for loops because they are directly applicable to your problem.
int Initialize( int init )
{
printf( "Initialize() : returning value of %d\n", init );
return init;
}
bool Condition( int & value, int limit )
{
bool result = ( value < limit );
const char * rstr = result ? "true" : "false";
printf( "Condition() : value is %d - returning %s\n", value, rstr );
return result;
}
void Increment( int & value, int incr )
{
value += incr;
printf( "Increment() : value is now %d\n", value );
}
void main()
{
const int init = -1; const int limit = 3; const int incr = 1;
printf( "beginning for loop : init is %d, limit is %d, incr is %d\n",
init, limit, incr );
int value;
for( value = Initialize( init ); Condition( value, limit ); Increment( value, incr ) )
{
printf( "for loop body : value is %d\n", value );
}
printf( "exited for loop - value is %d\n", value );
}
I recommend that you compile and run this program to see what happens. I made the statements functions so you can see the exact order of execution of each statement. You can also use a debugger and step through the program to see it in detail.
Another very useful thing to know is how a while loop differs from a for loop. The most important difference is a while has no incrementing statement which means a continue statement will act differently. In a for loop continue will cause execution to jump to the incrementer. In a while loop there isn't one so it goes back to the conditional statement. To see this in action, change the for loop to this and observe the behaviour :
for( value = Initialize( init ); Condition( value, limit ); Increment( value, incr ) )
{
if( value == 1 )
continue;
printf( "for loop body : value is %d\n", value );
}
With this in mind, a for loop can be thought of as a while loop. Here is the equivalent while loop :
printf( "beginning while loop : init is %d, limit is %d, incr is %d\n",
init, limit, incr );
int value = Initialize( init );
while( Condition( value, limit ) )
{
printf( "while loop body : value is %d\n", value );
Increment( value, incr );
}
printf( "exited while loop - value is %d\n", value );
Here is the while loop with the continue statement implement as a goto so it behaves like the for loop :
printf( "beginning while loop : init is %d, limit is %d, incr is %d\n",
init, limit, incr );
int value = Initialize( init );
while( Condition( value, limit ) )
{
if( value == 1 )
goto ContinueTarget;
printf( "while loop body : value is %d\n", value );
ContinueTarget :
Increment( value, incr );
}
printf( "exited while loop - value is %d\n", value );
Warning : if you change the goto statement to a continue you will wind up in an endless loop because the incrementer would be continually skipped and Condition would always return true.
Here's one last thing. Since you have seen how a for statement is equivalent to a while statement, here is how the for statement is equivalent to an if with a goto. Here is the initial for loop written with only if and goto :
init = Initialize( init );
LoopCondition:
if( ! Condition( value, limit ) )
goto LoopEnd;
printf( "for loop body : value is %d\n", value );
Increment( value, incr );
goto LoopCondition;
LoopEnd:
I hope you can see why people prefer to use for and while loops.
As a test for yourself, add code to last snippet of the for loop so it behaves like the previous one with the continue statement.