Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ternary operator as both left operative and right operative.. or neither

QUESTION IS: If this issue is clear to you, please explain to me what im not seeing. My question is: How does ternary actually work? To clarify my question: What does right to left associativity really mean here? Why is associativity not the same as order of evaluation? It is clearly like an if else statement. It is not evaluated from right to left. It seems to me to be left to right associative.

I made booleans to try and prove this. It shows me it isnt right associative.(I might not understand what right associative means.) If it was right associative, it would work like this, which was an answer which was given to me:

"Since this operator is right-associative, your code works as;"

true ? false ? false ? false ? 3 : 4 : 5 : 6 : 7
evaluated as;

true ? false ? false ? (false ? 3 : 4) : 5 : 6 : 7
which evaluated as;

true ? false ? false ? 4 : 5 : 6 : 7
which evaluated as;

true ? false ? (false ? 4 : 5) : 6 : 7
which evaluated as;

true ? false ? 5 : 6 : 7
which evaluated as;

true ? (false ? 5 : 6) : 7
which evaluated as;

true ? 6 : 7
which returns 6.

I tried to prove this, like this:

    int Proof = ternaryTrueOne() ? ternaryTrueTwo() ? ternaryFalseOne() ? 
ternaryTrueThree() ? ternaryFalseTwo() ? 2 : 3 : 4 : 5 : 6 : 7;
        static bool ternaryTrueOne()
    {
        Console.WriteLine("This is ternaryTrueOne");
        return true;
    }
    static bool ternaryTrueTwo()
    {
        Console.WriteLine("This is ternaryTrueTwo");
        return true;
    }
    static bool ternaryTrueThree()
    {
        Console.WriteLine("This is ternaryTrueThree");
        return true;
    }
    static bool ternaryFalseOne()
    {
        Console.WriteLine("This is ternaryFalse");
        return false;
    }
    static bool ternaryFalseTwo()
    {
        Console.WriteLine("This is ternaryFalseTwo");
        return false;
    }

In that case this would be evaluated in the same way. Right? That means ternaryfalsetwo would write first in to the console. But it doesnt. It doesnt write at all. It actually works like this, and ive written the ternary expression as an if statement. It works left to right, and it does not have to evaluate the rest of the code. All other statements are unreachable after the first false statement.

private static int Proof2()
{
    if (ternaryTrueOne())
    {
        if (ternaryTrueTwo())
        {
            if (ternaryFalseOne())
            {
                if (ternaryTrueThree())
                {
                    if (ternaryFalseTwo())
                    {
                        return 6;
                    }
                    else
                    {
                        return 7;
                    }
                    return 5;
                }
                else
                {
                    return 6;
                }
                return 4;
            }
            else
            {
                return 5;
            }
            return 3;
        }
        else
        {
            return 4;
        }
        return 2;
    }
    else
    {
        return 3;
    }
}

Was the original answer wrong? What does right associativity really mean?

like image 449
MakerOfTheUnicake Avatar asked Nov 26 '15 12:11

MakerOfTheUnicake


People also ask

Can ternary operator have two conditions?

In the above syntax, we have tested 2 conditions in a single statement using the ternary operator. In the syntax, if condition1 is incorrect then Expression3 will be executed else if condition1 is correct then the output depends on the condition2.

Which operator is used as ternary operator?

The conditional operator (? :) is a ternary operator (it takes three operands). The conditional operator works as follows: The first operand is implicitly converted to bool .

Is ternary operator right-associative?

Unlike most (all?) other languages, the ternary operator in PHP is left-associative rather than right-associative.

Why is ternary operator right-associative?

The operator = is right-associative, which means that a = b = c is equivalent to a = (b = c) , as opposed to (a = b) = c . Associativity has nothing to do with the order of evaluation. "Associativity has nothing to do with the order of evaluation." Well, unless all the operators have the same precedence.


1 Answers

Since the ternary conditional operator has its own place in the operator precedence table, (i.e. no other operator has exactly the same precedence as it), the associativity rule only applies when disambiguating a conditional operator from another.

Right to left associativity means that the implicit parentheses are around the rightmost ternary.

That is,

a ? b : c ? d : e

is equivalent to

a ? b : (c ? d : e).

https://en.wikipedia.org/wiki/Operator_associativity is a useful link.

like image 167
Bathsheba Avatar answered Nov 14 '22 23:11

Bathsheba