Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Objective-C Basics (Retired) Functional Programming in C While & Do While Loop

Shaun Kelly
Shaun Kelly
5,648 Points

infinite while loop

Anybody know how to code an infinite while loop that will print out a number but every time the code is repeated the number will be incremented by 1. So it will look like this as shown below...

1 2 3 4 5 6 7 8 9 10 11 12 13 14 . . . etc

This is what I have so far but the "number += number +1;" line doesn't do anything and it's printing out zero??

int i =0;
 do {
    int number = 0;
    printf("%d", number);
    number += number +1;

} while (i < 1);

1 Answer

Max Hirsh
Max Hirsh
16,773 Points

So, it looks like the way your code is written, the integer "number" is reassigned the value 0 every time the loop cycles through. Try assigning the value 0 to number outside of the loop first, then increment the number inside the loop.