-2

I created a for loop and followed the guidelines for ","s and ";"s like such

for (i = 0, j = 0; i < 10, j < 10; i++, j++) {
    //do something;
}

However I still got a warning when compiling saying that "left-hand operand of comma expression has no effect".

Carson147
  • 29
  • 4

3 Answers3

2

i < 10, j < 10

is allowed but probably isn't what was intended as it discards the result of the first expression and returns the result of j < 10 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.
(Collected) https://stackoverflow.com/a/17638765/5610655

If you want to test for multiple conditions use the logical AND operator && instead. Btw, comma-separated conditions run fine in my IDE.

Similar concepts have also been discussed previously. Have a look.
C++: Multiple exit conditions in for loop (multiple variables): AND -ed or OR -ed?
Are multiple conditions allowed in a for loop?

Here's a practical example. If you run the code given below, you'll see that it is outputting 10 times than 2 times

#include <stdio.h>
void main() 
    {
       int i,j;
       for (i = 0, j = 0; i < 2, j < 10; i++, j++) {
           printf("Hello, World!");
    }
}
--output----
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!

If you want both conditions to be effective, use AND(&&) operator instead of the comma-separated condition.

#include <stdio.h>
void main()
    {
        int i,j;
       for (i = 0, j = 0; i < 2 && j < 10; i++, j++) {
           printf("Hello, World!\n");
    }
}
---output--
Hello, World!
Hello, World!

It's unnecessary to use AND separated multiple conditions, only one will condition will suffice. The condition with fewer iteration steps will only be executed and the loop will break.

#include <stdio.h>
void main()
    {
        int i,j;
       for (i = 0, j = 0; i < 2; i++, j++) {
           printf("Hello, World!\n");
    }
}
---output--
Hello, World!
Hello, World!
0

Problem solved as pointed out by comments: "&&" instead of "," must be used between "i < 10" and "j < 10"

for (i = 0, j = 0; i < 10 && j < 10; i++, j++) {
    //do something;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Carson147
  • 29
  • 4
  • 3
    It's worth noting that a "double iterator" like this is really pointless unless `i` and `j` diverge in some meaningful way. In any case, do try and declare your loop variables within the loop scope, like `for (int i = 0, j = 0; ...)`. – tadman Jan 27 '21 at 02:54
0

the use of values in (i = 0, j = 0; i < 10, j < 10; i++, j++) is useless in the sense that both i and j will reach the same values at the same time. Unless one of the values may change at a time not within the declaration of the for loop, this can be simplified to (i = 0; i < 10; i++)

Also, as others have pointed out, the comma separating the comparisons (although valid) should be replaced with a &&. In the end, the loop should either be

for ( i = 0, j = 0; i < 10 && j < 10; i++, j++ )

or

for ( i = 0; i < 10; i++ )

Although you should prefer the latter as the former makes use of unnecessary code.

Raniconduh
  • 11
  • 1