FOR LOOPS

hi,
Is it wrong to construct a for loop like
for(i=1;i<=7;i+2)
{.....}
and if it is how can i increase it to go like 1,3,5,7 ??
i+=2 instead of i+2, and you'll be fine.
I think its like this:


for (int i=1;i<8,i++) {
while (i!=2||i!=4||i!=6) {
cout << i;
}
}



Or:

int i=1;
while (i!=2||i!=4||i!=6) {
i++;
cout << i;
}
darkrug is correct.

This is good:
1
2
3
4
for (int i=1; i<8; i+=2)
{
    cout << i << endl;
}


Sadly, both the suggestions from Joseph544310 contain errors. The for loop has a comma rather than a semicolon. But more importantly the while loops never terminate.

It would be possible to remedy one of the latter suggestions by using if (i%2) to verify whether or not the number is odd, but this method would not be my first choice.

Last edited on
Can we use double double plus?
Like:
1
2
3
4
for (int i=1; i<=7; ;i++++)
{
/*---Code---*/
}
@CyraxNguyen Nope.
At the beginning I thought of double double plus too:))It's good to learn that it doesnt work
Topic archived. No new replies allowed.