#include <iostream.h>
int main()
{
int n;
int i;
n = 1;
i = 0;
while(i < 11)
{
++i;
n = n + i;
cout << n;
if(i == 5) n = n + 10;
if(i != 10) cout << ", ";
}
system ("PAUSE");
return 0;
}
But the results are wrong: 2, 5, 9, 14, 30, 37, 45, 54, 6475
Why?
int n = 1;
for(int i = 0; i < 11; ++i)
{
n += i;
std::cout << n;
if(i == 5) n += 10;
if(i != 10) std::cout << ", ";
}
as a while loop would be:
1 2 3 4 5 6 7 8 9 10
int n = 1;
int i = 0;
while(i < 11)
{
n += i;
std::cout << n;
if(i == 5) n += 10;
if(i != 10) std::cout << ", ";
++i;
}
You should prefer the for loop, however, as it more directly represents what you're doing (iterating using a counter variable until a condition is met).