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?
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.
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 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++) { ... }
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.
The production for declaration
is:
declaration:
declaration-specifiers init-declarator-listopt;
So the semicolon is already part of it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With