简体   繁体   中英

Ternary operator inside a return statement

I was writing a recursive function, and encountered a problem at the return statement.

int SumOfEvenNumbers(int v[], int i)
{
    if ( i > v[0] )
        return 0;
    return SumOfEvenNumbers(v, i+1) + (v[i]%2==0)?v[i]:0;
}

The function is called like this:

SumOfEvenNumbers(vector_indexed_from_1, 1);
//v[0] is equal to the number of elements, excluding itself

What I expected the ternary if to return was either v[i] or 0 ( in case it is even ) but apparently after printing the result of every ternary if to the screen, the only values that result from the expression are 1 and 0.

int SumOfEvenNumbers(int v[], int i)
{
    if ( i > v[0] )
        return 0;
    cout << (v[i]%2==0)?(v[i]):(0); // either a 1 or a 0
    return SumOfEvenNumbers(v, i+1) + (v[i]%2==0)?v[i]:0;
}

The way I fixed this is by initializing a variable with the result of the expression and then adding that to the return value.

int SumOfEvenNumbers(int v[], int i)
{
    if ( i > v[0] )
        return 0;
    int rv = (v[i]%2==0)?(v[i]):(0);
    return SumOfEvenNumbers(v, i+1) + rv;
}

Could anyone explain what is happening? Is it possible to avoid declaring a variable in order to obtain the correct result?

The ternary conditional operator has lower precedence than operator+. Your code was actually parsed as

(SumOfEvenNumbers(v, i+1) + (v[i]%2==0)) ? v[i] : 0;

To get what you want, use parentheses

SumOfEvenNumbers(v, i+1) + ((v[i]%2==0)) ? v[i] : 0);

See: http://en.cppreference.com/w/c/language/operator_precedence

Could anyone explain what is happening?

Take a look at operator precedence . Here ternary conditional happens to have very low priority. In SumOfEvenNumbers(v, i+1) + (v[i]%2==0)?v[i]:0 expression it is calculated after + given you unexpected result.

Is it possible to avoid declaring a variable in order to obtain the correct result?

I cases when you are not sure about order of operators it is safe to use brackets () to indicate the order explicitly. So SumOfEvenNumbers(v, i+1) + ((v[i]%2==0)?v[i]:0) will be calculated as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM