Link to home
Start Free TrialLog in
Avatar of anuraggupta1
anuraggupta1

asked on

Behaviour of for loop

please tell me the behaviour of following for loop

main()
{
  int i ;
  for(i=1; i<10,printf("%d",i) ; i++) ;
}

Compare it with

main()
{
  int i ;
  for(i=1; printf("%d",i),i<10 ; i++) ;
}

Thanks.
Avatar of sunnycoder
sunnycoder
Flag of India image

printf returns the number of elements printed ... The first for loop will never end since printf will always return non-zero which will keep the cnodition true and loop will keep printing ever increasing values

Second loop will simply print 1 to 10 and terminate since here it will be i<10 which will affect loop termination. (the second expression after , is controlling loop exit)
Looks like some kind of homework question... or a dirty programming trick.
But is totally unusual, best practice is:

main()
{
  int i;
  for(i=1; i<10; i++)
        printf("%d",i);
}

Some tips:
expressions (in the middle of for(;;) are evaluated always from left to right.
so you just have to use pen and paper and make a simulation (just need i<4 to understand clearly)
Another tip:
 printf("%d",i) will return 1 or 2, since it returns the number of characters printed, so it is evaluated as logical TRUE
Avatar of anuraggupta1
anuraggupta1

ASKER

thanks all for answers.
Yes, jaime_olivares it is dirty trick.
but i am just improving my c skills.

Why does the 2nd experssion given after , controls the loop.
, operator does not work here ?

please reply.
i am confused.
thanks.
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The for statement:
for(A; B; C) {
    D;
}

is exactly equivalent to this:
A;
while (B) {
    D;
    C;
}

Which is exactly equivalent to this:
A;
COND:  if (!B) goto LOOPEND;
D;
C;
goto COND;
LOOPEND:
main()
{
  int i ;
  for(i=1; i<10,printf("%d",i) ; i++) ;
}

The above is an infinite loop.  The for loop format is
for(A; B; C) where A is used for initializations and occurs only once.  B is the conditional statement is done at the beginning of each loop.  C is done at the end of each loop.  

When using the Comma operator as stated by Sunny coder stated the the right most condition is used (but all the conditions are evaluated).  In C anything that evaluates to 0 is considered false.  ALL OTHER VALUES are considered true; thus the printf("%d", i) always evaluates to true.  If you set i = 0 then only 0 would be printed.  But they way you have it now it prints
1234567891011...

Compare it with

main()
{
  int i ;
  for(i=1; printf("%d",i),i<10 ; i++) ;
}

This prints
12345678910

Again the right most value in the comma separated expression is used to determine the value condition.  In this case i < 10 is used since it is the right most expression in the list.  Notice that this loop does print the number 10.  The reason for this is when the conditional is being tested to see if the loop should continue printf("%d", i) is evaluated first.  Then i < 10 is evaluated.  10 is not less than 10 so the for loop quits.
What other information you required? B grade indicates an incomplete but useful answer

https://www.experts-exchange.com/help.jsp#hi73
B: The Expert(s) provided an acceptable solution, or a link to an acceptable solution, that you were able to use, although you may have needed a bit more information to complete the task.