Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to create a for loop with only two expressions in C?

Tags:

c

for-loop

I came across this in the C standard text, any version I can find for C99 or C11, at §6.8.5 :

iteration-statement:
  while ( expression ) statement
  do statement while ( expression ) ;
  for ( expression ; expression ; expression ) statement
  for ( declaration expression ;  expression ) statement

The fourth item here seems to be a for with only one semicolon, and I don't see any reference to this syntax anywhere else. Can anyone here explain what am I missing?

like image 722
Gábor Buella Avatar asked Apr 19 '14 10:04

Gábor Buella


People also ask

Can a for loop have 2 conditions?

It do says that only one condition is allowed in a for loop, however you can add multiple conditions in for loop by using logical operators to connect them.

Is it necessary to have all the three expressions in a for loop?

All these components are optional. In fact, to write an infinite loop, you omit all three expressions: for ( ; ; ) { //infinite loop ... } The output of the program is: 32 87 3 589 12 1076 2000 8 622 127 .

Can you have two variables in a for loop in C?

Can a for loop in C refer to two variables in its loop-condition expression? Yes, by using the comma operator. E.g. for (i=0, j=0; i<10; i++, j++) { ... }

Can we increment 2 variables in for loop?

Yes, C Compiler allows us to define Multiple Initializations and Increments in the “for” loop. The image below is the program for Multiple Initializations and Increments. In the above program, two variable i and j has been initialized and incremented. A comma has separated each initialization and incrementation.


1 Answers

The production for declaration is:

declaration:
    declaration-specifiers init-declarator-listopt;

So the semicolon is already part of it.

like image 161
nwellnhof Avatar answered Sep 25 '22 03:09

nwellnhof