[Solved] Is this structure valid? [closed]


The ternary conditional expression produces a value. You must do something with that value (assign it to a variable, return it from your method, etc…).

In your case, you are assigning to one of two different variables based on a condition.
It doesn’t make much sense and a normal if-else would be more readable:

int sumEven = 0;
int productoImpares = 1;
for(int i=0; i<=20; i++) {
    if (i % 2 == 0) {
        sumEven += i;
    } else {
        productoImpares *= i;
    }
}

Now, if you insist on using the ternary conditional operator, it can be done as follows:

int sumEven = 0;
int productoImpares = 1;
for(int i=0; i<=20; i++) {
    int x = (i % 2 == 0) ? (sumEven += i) : (productoImpares *= i);
}

Note the dummy variable x I used to assign the value of the ternary condition expression. I advise against it.

solved Is this structure valid? [closed]