Restating what Salem said, because we're just not going to do your homework for you...
A for loop works as follows:
Code:
for (set initial condition; set loop-terminating condition; end-of-loop operation)
{
do work;
}
Breaks down rather simply into a while loop:
Code:
set initial condition;
while (loop-terminating condition is not met)
{
do work;
do end-of-loop operation;
}
Some example loops:
Code:
// Infinite for loop
for(;;) {}
// Infinite while loop
while(1) {}
// Infinite do loop
do {} while (1);
// Unintentionally infinite while loop
const char * const p = "Please remember to update with respect to your loop-terminating condition";
const char *q = p;
while (*q != '\0')
{
std::cout << *q;
}
// Above, fixed one way
const char * const p = "Please remember to update with respect to your loop-terminating condition";
const char *q = p;
while (*q != '\0')
{
std::cout << *q;
++q;
}
// And a different way
const char * const p = "Please remember to update with respect to your loop-terminating condition";
const char *q = p;
while (*q != '\0')
{
std::cout << *q++;
}
// And another, different way
const char * const p = "Please remember to update with respect to your loop-terminating condition";
const char *q = p;
while (*q++ != '\0')
{
std::cout << *q;
}