tags:

views:

10565

answers:

6

I have seen some very weird for loops when reading other people's code. I have been trying to search for a full syntax explanation for the for loop in C but it is very hard because the word "for" appears in unrelated sentences making the search almost impossible to Google effectively.

This question came to my mind after reading this thread which made me curious again.

The for here:

for(p=0;p+=(a&1)*b,a!=1;a>>=1,b<<=1);

In the middle condition there is a comma separating the two pieces of code, what does this comma do? The comma on the right side I understand as it makes both a>>=1 and b<<=1.

But within a loop exit condition, what happens? Does it exit when p==0, when a==1 or when both happen?

It would be great if anyone could help me understand this and maybe point me in the direction of a full for loop syntax description.

+5  A: 

The comma simply separates two expressions and is valid anywhere in C where a normal expression is allowed. These are executed in order from left to right. The value of the rightmost expression is the value of the overall expression.

for loops consist of three parts, any of which may also be empty; one (the first) is executed at the beginning, and one (the third) at the end of each iteration. These parts usually initialize and increment a counter, respectively; but they may do anything.

The second part is a test that is executed at the beginning of each execution. If the test yields false, the loop is aborted. That's all there is to it.

Konrad Rudolph
The way you worded your sentence "one (the first) is executed at the beginning, and one (the third) at the end of each iteration" might lead some people to think that the first part is executed at the beginning of ever iteration.
quikchange
+62  A: 

The comma is not exclusive of for loops; it is the comma operator.

x = (a, b);

will do first a, then b, then set x to the value of b.

The for syntax is:

for (init; condition; increment)
    ...

Which is somewhat (ignoring continue and break for now) equivalent to:

init;
while (condition) {
    ...
    increment;
}

So your for loop example is (again ignoring continue and break) equivalent to

p=0;
while (p+=(a&1)*b,a!=1) {
    ...
    a>>=1,b<<=1;
}

Which acts as if it were (again ignoring continue and break):

p=0;
p+=(a&1)*b;
while (a!=1) {
    ...
    a>>=1;
    b<<=1;
    p+=(a&1)*b;
}

Two extra details of the for loop which were not in the simplified conversion to a while loop above:

  • If the condition is omitted, it is always true (resulting in an infinite loop unless a break, goto, or something else breaks the loop).
  • A continue acts as if it were a goto to a label just before the increment, unlike a continue in the while loop which would skip the increment.

Also, an important detail about the comma operator: it is a sequence point, like && and || (which is why I can split it in separate statements and keep its meaning intact).

CesarB
ty, exactly what i wanted to know :)
fmsf
You just answered one of my favorite interview questions better than anyone I've ever interviewd! +2, if I were able.
Adam Liss
A very clear answer. Well done!
Tom Leys
In C, comma has lower precedence than assignment, so "x = a, b;" will first set x to a, then do b.
ephemient
@ephemient: oops. You are right. Fixed, thanks!
CesarB
Does X actually get set to a? or is it that a is evaluated, discarded, b is evaluated then assigned?
EvilTeach
It might also be worth mentioning that the commas separating function arguments are not comma operators, so they do not act as sequence points.
Michael Burr
@EvilTeach: X is not set to a. (a, b) evaluates a completely, then evaluates b completely and returns b.
David Thornley
@DavidThornley what if it had been x = a, b; rather than (a, b)?
TM
+1 Awesome Explanation.
Christian Witts
@TM: if I've understood things correctly, "x = a, b" will assign a to x, then evaluate b and discard its value.
onnodb
+4  A: 

The C style for loop consists of three expressions:

for (initializer; condition; counter) statement_or_statement_block;
  • The initializer runs once, when the loop starts.
  • The condition is checked before each iteration. The loop runs as long it evaluates to true.
  • The counter runs once after each iteration.

Each of these parts can be an expression valid in the language you write the loop in. That means they can be used more creatively. Anything you want to do beforehand can go into the initializer, anything you want to do in between can go into the condition or the counter, up to the point where the loop has no body anymore.

To achieve that, the comma operator comes in very handy. It allows you to chain expressions together to form a single new expression. Most of the time it is used that way in a for loop, the other implications of the comma operator (e.g. value assignment considerations) play a minor role.

Even though you can do clever things by using syntax creatively - I would stay clear of it until I find a really good reason to do so. Playing code golf with for loops makes code harder to read and understand (and maintain).

The wikipedia has a nice article on the for loop as well.

Tomalak
+5  A: 

C++ allows overloading the comma operator. Seriously.

It does? That's a nice little WTF here ...
Joachim Sauer
Personally, my "WTF" was neither nice nor little.
Chris Lutz
A: 

Everything is optional in a for loop. We can initialize more than one variable, we can check for more than one condition, we can iterate more than one variable using the comma operator.

The following for loop will take you into an infinite loop. Be careful by checking the condition.

for(;;)
A: 

Konrad mentioned the key point that I'd like to repeat:

The value of the rightmost expression is the value of the overall expression.

A Gnu compiler stated this warning when I put two tests in the "condition" section of the for loop

warning: left-hand operand of comma expression has no effect

What I really intended for the "condition" was two tests with an "&&" between. Per Konrad's statement, only the test on to the right of the comma would affect the condition.

Bob