FOR LOOPS

Oct 27, 2012 at 7:05pm
hi,
Is it wrong to construct a for loop like
for(i=1;i<=7;i+2)
{.....}
Oct 27, 2012 at 7:06pm
and if it is how can i increase it to go like 1,3,5,7 ??
Oct 27, 2012 at 7:21pm
i+=2 instead of i+2, and you'll be fine.
Oct 27, 2012 at 7:24pm
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;
}
Oct 27, 2012 at 7:44pm
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 Oct 27, 2012 at 7:46pm
Oct 27, 2012 at 7:46pm
Can we use double double plus?
Like:
1
2
3
4
for (int i=1; i<=7; ;i++++)
{
/*---Code---*/
}
Oct 27, 2012 at 7:58pm
@CyraxNguyen Nope.
Oct 27, 2012 at 8:08pm
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.