Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
2.00/5 (4 votes)
See more:
what will happen if we exchange the postion of loop condition and increment expression in "for loop" in c

for(i=0;i++;i<5)
and
for(i=0;i<5;i++)


what is the difference betwwen these two.
Posted
Updated 17-Feb-18 4:46am
Comments
Kenneth Haugland 13-Aug-12 16:02pm    
One will work and one not...
[no name] 13-Aug-12 16:04pm    
Good thing I didn't say that. But I was thinking it.
Kenneth Haugland 13-Aug-12 16:06pm    
???
[no name] 13-Aug-12 16:09pm    
Would have seemed weird for both of us to say the same thing at the same time.
Kenneth Haugland 13-Aug-12 16:14pm    
lol ... Great minds think a like... or somthing... I just thought what Chris said also... Why not try? Didnt get that....

Hey, here is something you should know: Those thingies separated by ';' characters in the head of the for loop are expressions, all three of them! Both for loops compile but this should give a warning: for(i=0;i++;i<5) because the last expression doesn't have a side effect (ie: it does not change anything) and your compiler is smart! As I said all three thingies are expressions but the first is used for initialization, the second is for pre-testing before every execution of the loop-body, and the third expression is executed after each execution of the loop-body.

This c code:
C++
for (expr1; expr2; expr3)
{
   code_in_the_body_of_for_loop
}


Can be written this way:
C++
expr1            // initialization, like i=0; the compiler should warning if this
                 // has no side effect (if it doesnt change anything like i<5)

while (1)        // infinite loop
{
    if (!expr2)  // pre testing before execution of the loop-body
        break;

                 // this is the code inside the for loop, note that
                 // a "continue" statement in the body of the  for loop
                 // jumps to expr3 and not to the beginning of the for loop!!!
    code_in_the_body_of_for_loop

    expr3;       // usually some expressions that change the loop-counter
                 // like i++ or i+=5 or ptr = ptr->parent;
                 // the compiler should warning if this has no side effect.
}


In your case when the pre-test expression (expr2) is i++ the for loop never executes the body code because the value of i++ is zero so it breaks out from your for loop immediately! You compiler should also warning if expr3 is just i<5 because you normally never write a statement like i<5; in your code, do you? :D
 
Share this answer
 
v3
Comments
Kenneth Haugland 13-Aug-12 18:27pm    
So in binary: 1 - 0 ? :-D
CPallini 21-Aug-13 13:18pm    
Wow, great!.
5+
pasztorpisti 13-Aug-12 18:30pm    
Yep, thats almost the same! :-)
pasztorpisti 21-Aug-13 13:21pm    
Reverse engineering C in C... :-)
Kenneth Haugland 13-Aug-12 18:35pm    
I'll give you a 5 :)
The difference is that one is valid C and the other is not going to compile at all. Why would you ask this, and not just type it in and see what happens ? Why ask what something does without trying it for yourself to see ?
 
Share this answer
 
This is the right format in case of a for loop.
for(i=0;i<5;i++)

If you try with other one you mentioned..
for(i=0;i++;i<5)

The compiler will not compile it and point error here.Because it conflicts the grammar of a for loop.
 
Share this answer
 
Comments
pnr4code 17-Feb-18 10:54am    
Compiler will compile. but it will not end.
Richard MacCutchan 17-Feb-18 11:40am    
You came all this way just to post that comment on a FIVE year old question?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900