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! While you're at it, check out some resources Treehouse students have shared here.

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

Need some help, please!

Hello, I am quite confused about the following pieces of rather elementary loops:

''' int main() {

   int  i;

    i = 0;
while ( i < 4 ) {
        printf( "while: i=%d\n", i );

     ++i; }

    printf( "After while loop, i=%d.\n\n", i );


    return 0;

} '''

and

'''

int main() {

   int  i;

    i = 0;
while ( i++ < 4 ) 
        printf( "while: i=%d\n", i );

    printf( "After while loop, i=%d.\n\n", i );

    return 0;

}

'''

Both of these examples compile correctly, but the results are different:

for the first example, the console output is:

  • while: i=1
  • while: i=2
  • while: i=3
  • while: i=4
  • After while loop, i=5.

  • Program ended with exit code: 0

And for the second example the result is:

  • while: i=0
  • while: i=1
  • while: i=2
  • while: i=3
  • After while loop, i=4.

  • Program ended with exit code: 0

I can imagine it has something to do with postfix vs prefix annotation of ++ (++i vs i++) but I ca't exactly put my finger on it. Could you please try to explain in a step by step manner?

THANK YOU!

2 Answers

A while loop is processed one command at a time. The reason why the results are different is because in program #1 you are auto incrementing before the loop checks the value of i.

while ( i++ < 4 )

i = 0 then you add one to i. i = 1, is i less than 4? Yes, so the program lets the loop run. when it runs again it increments again and checks the value. i = 1 + 1. i is now 2. This runs two more times. Is i less than 4? Yes, because it is equal to four and it runs again. After it has run four times the value is 5. Is i less than 4? No, so it stops the loop and then displays the 5. After while loop, i=5.

In program #2 the program assigns 0 to i then if i is less than 4, execute code block then it runs again. By the time you have run the loop 4 times i will = 4, so the loop reads it again. Is i less than 4? At this point i = 4, which is equal to 4, so it runs through one more time and sets i = 5. The loop checks i again. Is i less than 4? No, so it prints 4 without incrementing, because that only happens when i is less than four in your program. After your loop, the program will print the value of i which is now 4. After while loop, i=4.

the postfix and prefix annotation of ++ it's important only when you assign the value to a variable like k = i++; vs k = ++i or in a condition like the while loop.

The 2 programs have 2 different output because of the condition in the while loop:

while ( i++ < 4 ) means that the variable "i" is compared to the condition ( < 4 ) with value of 0 , then it is incremented to the value of 1 and the first time the printf is called i is equal to 1

if you try to write while ( ++i < 4 ) the variable "i" is incremented to the value of 1 and then compared to < 4.