For Loop to While Loop

closed account (y6DLy60M)
for (int i=2; i<=100; i=i+3)
cout << i << "\n";

Im trying to write this as a while loop but it increments wrong in my program.

int i = 2;
while(i <= 100)
cout<< i ;
cout<< "\n";
i = i++ + 3;

That's because you added an increment operator to i. Change that last line to i += 3 and it'll work just like before.

Also, please use the code tags (the <> button to the right under Format: when you post or edit a post) as it makes code much easier to read. :)
Last edited on
closed account (y6DLy60M)
It just adds 6 now .. lol
Huh? How? i += 3 says take the value in i, add 3 to it and assign it to i. How can it add 6?
closed account (y6DLy60M)
I have no idea . My program is just like it is above but I changed the last line.
It actually just contiously adds 3
Right, because that's what you coded it to do. What are you trying to do?
closed account (y6DLy60M)
I want it to make the for loop a while loop.
It's my first loop in the post
Right. That for loop increments the same way I showed. So how do you want it to increment? It might be better if you posted some example output.
Last edited on
closed account (y6DLy60M)
Oh I'm so sorry. I must be dyslexic the code was the same it looked different at first.
thank you sir
Ya, what I wrote was just a little more compact way of saying what you did. IE, i = i + 3 is the same as i += 3. Good luck!
Topic archived. No new replies allowed.