9

The following code runs without giving any errors or warnings:

#include <stdio.h>

int main(){
    int i, j;
    int p = 0, q = 2;
    for(i = 0, j = 0; i < p, j < q; i++, j++){
        printf("Hello, World!\n");
    }
    return 0;
}

However, the book Let Us C (Yashwant Kanetkar) says that only one expression is allowed in the test expression of a for loop (see page 115 of the book).

I am not sure of the standard. Are multiple expressions allowed in the test expression of a for loop?

I surely can join the two expressions, but I was dumbstruck when I found the above code on this website. Is this valid C code or not?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nikunj Banka
  • 11,117
  • 16
  • 74
  • 112
  • 2
    yeah I surely can but I was dumbstruck when I found the above code on the website : http://www.geeksforgeeks.org/output-of-c-program-set-22/ – Nikunj Banka Jul 14 '13 at 10:54
  • C standards are diffrent e.g. C89 and C99. Beside there are variations in diffrent implementations of C language compilers. Your code is correct. – Farshid Jul 14 '13 at 10:54
  • this code is correct - a `comma` operator evaluates its operands and its result is the last operand. e.g. `int a = 1,2; //here a = 2` – Max Komarychev Jul 14 '13 at 11:04
  • I suggest you don't read Let Us C, have a look on this [The Definitive C Book Guide and List](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – Grijesh Chauhan Jul 14 '13 at 11:07
  • 1
    Enable all warnings. gcc with `-Wall` warns about this. – ugoren Jul 14 '13 at 11:29
  • 3
    @NikunjBanka you have no reason to have been dumbstruck regarding that code snippet you found, nor to have posted about it here. All you had to do is bother to read beyond the code on that page... particularly the description of the code which reads: _When two expressions are separated by comma operator, the first expression (i < p) is executed first. Result of the first expression is ignored._ – mah Jul 14 '13 at 11:30
  • A canonical question is *[Multiple conditions in a C 'for' loop](https://stackoverflow.com/questions/16859029/)*. But there must be one from 2008 or 2009. – Peter Mortensen Sep 01 '22 at 22:55

5 Answers5

21

The condition

i < p, j < q

is allowed but probably isn't what was intended as it discards the result of the first expression and returns the result of j < q only. The comma operator evaluates the expression on the left of the comma, discards it then evaluates the expression on the right and returns it.

If you want to test for multiple conditions use the logical AND operator && instead

i < p && j < q
simonc
  • 41,632
  • 12
  • 85
  • 103
8

You can link them together with boolean and (&&)

for(i = 0, j = 0; (i < p) && (j < q); i++, j++){

The above won't print out anything in the loop because the (i < p) condition fails immediately as i & p are both the same value (0).

Update: Your example is valid (but silly) C because if you start i=30 your loop will still execute 2 times, because the first result in a comma separated list is ignored.

vogomatix
  • 4,856
  • 2
  • 23
  • 46
3

If you want to test both conditions, use the && operator.

What is happening in your code is related to how the comma operator , works.

Both i < p and j < q are evaluated, but only the result of the 2nd expression j < q is checked by the for loop.

Nithin Bhaskar
  • 686
  • 1
  • 5
  • 9
2
for(i = 0, j = 0; i < p && j < q; i++, j++){
1

Even I have read That Book by Mr Yashwant Kanetkar. 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. In other books that I have Read Along time Ago, said that only one condition is allowed.

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295