• C

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.
anuraggupta1Asked:
Who is Participating?
 
sunnycoderCommented:
>, operator does not work here ?
It does work. What are the semantics of , operator?

The comma-separated expressions are evaluated from left to right and the value of the whole comma-separated sequence is the value of the rightmost expression in the sequence
http://crasseux.com/books/ctutorial/The-comma-operator.html
0
 
sunnycoderCommented:
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)
0
 
Jaime OlivaresSoftware ArchitectCommented:
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)
0
Managing Security & Risk at the Speed of Business

Gartner Research VP, Neil McDonald & AlgoSec CTO, Prof. Avishai Wool, discuss the business-driven approach to automated security policy management, its benefits and how to align security policy management with business processes to address today's security challenges.

 
Jaime OlivaresSoftware ArchitectCommented:
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
0
 
anuraggupta1Author Commented:
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.
0
 
NovaDenizenCommented:
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:
0
 
PerryDKCommented:
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.
0
 
sunnycoderCommented:
What other information you required? B grade indicates an incomplete but useful answer

http://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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.