Take the 2-minute tour ×

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:


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.

share|improve this question
@Jesus -- You corrected "kinda" to "kind of", "coma" to "comma", capitalized the "I" and "Does" but left "ppl" for people? –  James Caccese Mar 24 '09 at 20:25
Corrected some errors. Post looks much better now. Interesting question. –  George Stocker Mar 30 '09 at 15:02

7 Answers 7

up vote 110 down vote accepted

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:

while (condition) {

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

while (p+=(a&1)*b,a!=1) {

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

while (true) {
    if (a == 1) break;

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).

Changes in C99

The C99 standard introduces a couple of nuances not mentioned earlier in this explanation (which is very good for C89/C90).

First, all loops are blocks in their own right. Effectively,

for (...) { ... }

is itself wrapped in a pair of braces

for (...) { ... }

The standard sayeth:

ISO/IEC 9899:1999 §6.8.5 Iteration statements

¶5 An iteration statement is a block whose scope is a strict subset of the scope of its enclosing block. The loop body is also a block whose scope is a strict subset of the scope of the iteration statement.

This is also described in the Rationale in terms of the extra set of braces.

Secondly, the init portion in C99 can be a (single) declaration, as in

for (int i = 0; i < sizeof(something); i++) { ... }

Now the 'block wrapped around the loop' comes into its own; it explains why the variable i cannot be accessed outside the loop. You can declare more than one variable, but they must all be of the same type:

for (int i = 0, j = sizeof(something); i < j; i++, j--) { ... }

The standard sayeth:

ISO/IEC 9899:1999 § The for statement

The statement

for ( clause-1 ; expression-2 ; expression-3 ) statement

behaves as follows: The expression expression-2 is the controlling expression that is evaluated before each execution of the loop body. The expression expression-3 is evaluated as a void expression after each execution of the loop body. If clause-1 is a declaration, the scope of any variables it declares is the remainder of the declaration and the entire loop, including the other two expressions; it is reached in the order of execution before the first evaluation of the controlling expression. If clause-1 is an expression, it is evaluated as a void expression before the first evaluation of the controlling expression.133)

Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant.

133) Thus, clause-1 specifies initialization for the loop, possibly declaring one or more variables for use in the loop; the controlling expression, expression-2, specifies an evaluation made before each iteration, such that execution of the loop continues until the expression compares equal to 0; and expression-3 specifies an operation (such as incrementing) that is performed after each iteration.

share|improve this answer
ty, exactly what i wanted to know :) –  fmsf Nov 9 '08 at 21:50
You just answered one of my favorite interview questions better than anyone I've ever interviewd! +2, if I were able. –  Adam Liss Nov 9 '08 at 22:27
A very clear answer. Well done! –  Tom Leys Nov 9 '08 at 22:32
@EvilTeach: X is not set to a. (a, b) evaluates a completely, then evaluates b completely and returns b. –  David Thornley Mar 9 '09 at 14:12
@onnodb: yes, because the precedence of the comma operator is even lower than the precedence of the assignment operator. –  Jonathan Leffler May 29 '11 at 22:32

C++ allows overloading the comma operator. Seriously.

share|improve this answer
It does? That's a nice little WTF here ... –  Joachim Sauer Mar 3 '09 at 21:22
Personally, my "WTF" was neither nice nor little. –  Chris Lutz Mar 3 '09 at 21:23

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.

share|improve this answer
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 Apr 16 '09 at 23:31

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.

share|improve this answer
Is another for loop allowed in the counter section, which would allow nested for loops to be defined in the same statement? In other words, how is a compound for loop defined in C/C++? –  lifebalance Sep 7 '14 at 15:21
I'm not enough of a C language lawyer to answer that. Interesting and original question nonetheless. There's a good chance for an answer if you ask that as an actual question here. –  Tomalak Sep 7 '14 at 15:25
And I took your advice, here's an answer. And please upvote the question if you feel so. Enjoy! –  lifebalance Sep 7 '14 at 18:25

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.

share|improve this answer

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.

share|improve this answer
what do you mean by "The value of the rightmost expression is the value of the overall expression."? I guess you mean "The value of the rightmost expression is the value of the overall comma expression" ? –  Allan Ruin May 27 '12 at 9:11

the for loop is execution for particular time for(;;)

the syntex for for loop



for (initializer; condition; counter)

e.g (rmv=1;rmv<=15;rmv++)

execution to 15 times in for block

1.first initializ the value because start the value

(e.g)rmv=1 or rmv=2

2.second statement is test the condition is true or false ,the condition true no.of time execution the for loop and the condition is false terminate for block,

e.g i=5;i<=10 the condition is true

i=10;i<10 the condition is false terminate for block,

3.third is increment or decrement

(e.g)rmv++ or ++rmv

share|improve this answer
Your formatting is horrible. You should write code in monospace. To make a block of code, indent each line belonging to it using 4 spaces. To make an inline monospace like this, use ` at the beggining and at the end of text you want monospace. –  GingerPlusPlus Oct 14 '14 at 13:28
Also, for(;;) is not alternative syntax; it's normal syntax, with skipped initializer, condition and counter. –  GingerPlusPlus Oct 14 '14 at 13:30

Your Answer


