Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the full "for" loop syntax in C?

Tags:

c

syntax

for-loop

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.

like image 340
fmsf Avatar asked Nov 09 '08 21:11

fmsf


People also ask

What is the syntax of for loop in C?

The syntax of for loop in c language is given below: for(Expression 1; Expression 2; Expression 3){ //code to be executed. }

What is the syntax of for loop?

Syntax of a For LoopThe for loop starts with a for statement followed by a set of parameters inside the parenthesis. The for statement is in lower case. Please note that this is case sensitive, which means the for command always has to be in lower case in C programming language.

What Is syntax of for loop give example?

A "For" Loop is used to repeat a specific block of code a known number of times. For example, if we want to check the grade of every student in the class, we loop from 1 to that number.

What is for loop in C language?

The for loop in C language is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like the array and linked list.


1 Answers

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;  while (true) {     p+=(a&1)*b;     if (a == 1) break;     ...     a>>=1;     b<<=1; } 

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 §6.8.5.3 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.

like image 196
CesarB Avatar answered Sep 25 '22 01:09

CesarB