Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
My book Question 10

10. Which one of the following statements is not equivalent to the other two (assuming that the loop bodies are the same)?

a) for (i =0; i < 10; i++)
b) for (i =0; i < 10; ++i)
c) for (i =0; i++ < 10; )

I choose c, obviously it looks different, but the expr2 which is the control expression is exactly the same for a and b. and they start testing with i = 0. Option c increments i, (adds 1 to i) before the test. So we're not testing 0 < 10 like a and b. We're testing 1 < 10.

Is this the correct answer and do I have the correct reason?
 
My book Question 10

10. Which one of the following statements is not equivalent to the other two (assuming that the loop bodies are the same)?

a) for (i =0; i < 10; i++)
b) for (i =0; i < 10; ++i)
c) for (i =0; i++ < 10; )

I choose c, obviously it looks different, but the expr2 which is the control expression is exactly the same for a and b. and they start testing with i = 0. Option c increments i, (adds 1 to i) before the test. So we're not testing 0 < 10 like a and b. We're testing 1 < 10.

Is this the correct answer and do I have the correct reason?

Wrong reasoning. i++ increases i by one, but yields the old value. Each of the three loops will be executed exactly ten times (unless there is something in the loop body).

But consider the value of i within the body of the loop: What would be the output of
Code:
printf ("%d\n", i);
?
 
C is the correct answer, but not for the reason you stated. The short of it is, A and B increment 'i' after printing it, C increments it before printing.

Set up a small test program, compile it and test it so you can see what happens. That is the best way to learn.

Code:
#include <stdio.h>

int main () {

    int i;
    for (i =0; i < 10; i++) {
        printf("%d ",i);
    }
    printf("\n");
    for (i =0; i < 10; ++i) {
        printf("%d ",i);
    }
    printf("\n");
    for (i =0; i++ < 10; ) {
        printf("%d ",i);
    }
    printf("\n");
    return 0;
}

There are 100 different ways of doing different types of counting in C (and related languages). Obviously answer C in this case is not very clear, so you would want to pick a method that is easy to read and understand while accomplishing what you want.
 
Last edited:
Write this out on paper:

a) for (i =0; i < 10; i++)
First step: i is assigned the value 0
Second step: i is compared to 10, i is 0, pass.
Third step: Execute loop body, i is 0
Fourth step: i++, i is now 1
Fifth step: i is compared to 10, i is 1, pass
Sixth step: execute loop body, i is 1
Seventh step: i++, i is set to 2

b) for (i =0; i < 10; ++i)
First step: i is assigned the value 0
Second step: i is compared to 10, i is 0, pass.
Third step: Execute loop body, i is 0
Fourth step: ++i, i is now 1
Fifth step: i is compared to 10, i is 1, pass
Sixth step: execute loop body, i is 1
Seventh step: ++i, i is set to 2

c) for (i =0; i++ < 10; )
First step: i is assigned the value 0
Second step: i++ is compared to 10, i++ evaluates to 0, so 0 < 10 , pass. i set to 1
Third step: Execute loop body, i is 1
Fourth step: empty statement, i is now 1
Fifth step: i++ is compared to 10, i++ evaluates to 1, so 1 < 10 , pass. i set to 2


You can tell by the time you execute the loop body for the first time that C is different, as i is 1 for the first iteration, not 0 like the other two. I stopped, but it should take a few minutes for you to go through all 100 or so steps. At the end you can see what i's value is, and what the value of i was while executing the loop body each time.

-Lee
 
As has been mentioned in another thread, for is just convenient syntax for a while loop. Like this:

Code:
for ( [I][COLOR="Green"]start[/COLOR][/I] ; [I][COLOR="DarkOrange"]test[/COLOR][/I] ; [I][COLOR="Purple"]cycle[/COLOR][/I] ) { [I][COLOR="blue"]loop body[/COLOR][/I]; )

// expands to:

[I][COLOR="green"]start[/COLOR][/I]; // initialize the variable
while ( [I][COLOR="DarkOrange"]test[/COLOR][/I] ) {
    [I][COLOR="Blue"]loop body[/COLOR][/I]; // all the statements in the loop
    [I][COLOR="purple"]cycle[/COLOR][/I]; // increment the variable, or whatever
}

This probably does not help you, but then again, it might.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.