Solved

# Behaviour of for loop

Posted on 2005-05-16
281 Views
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.
0
Question by:anuraggupta1

LVL 45

Expert Comment

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

LVL 55

Expert Comment

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

LVL 55

Expert Comment

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

Author Comment

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 ?

i am confused.
thanks.
0

LVL 45

Accepted Solution

>, 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

LVL 22

Expert Comment

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

LVL 4

Expert Comment

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

LVL 45

Expert Comment

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

## Featured Post

### Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.