user4221591
user4221591

Reputation: 2180

computing the output of ternary operation

I am trying to solve this problem, but can not find the correct way of doing this.

int a = 5, b = 10, c = 20; 
int result = a < b ? a < c ? c : a : b; 
Console.WriteLine(result);

Please suggest how can I compute this problem.

Upvotes: 0

Views: 115

Answers (3)

displayName
displayName

Reputation: 14399

The code you have given:

int a = 5, b = 10, c = 20; 
int result = a < b ? a < c ? c : a : b; 
Console.WriteLine(result);

transaltes to:

int a = 5, b = 10, c = 20; 
int result;
if (a < b)
{ 
    if (a < c)
    {
        result = c;
    }
    else
    {
        result = a;
    }
}
else
{
    result = b;
}
Console.WriteLine(result);

Therefore, the problem is that if a is less than b, then without checking b against c, you are setting result to b.

If you insist on using ternary operators, the condition should be:

int result = a < b ? (a < c ? a : (b < c ? b : c)) : (b < c ? b : c);

The translation is simple. Let me know if you want it elaborated.

Upvotes: 0

Christos
Christos

Reputation: 53958

If you write your expression like below, then it will be more clear:

int result = (a < b ) ? ((a < c ) ? c : a) : b;

What does the conditional operator?

The conditional operator (?:) returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator.

condition ? first_expression : second_expression;

For further documentation on this, please have a look here.

Now, let's do the calculation in "paper":

a < b

is true. So, the condition a < c will be evaluated. This condition is also true. Hence, the result would be equal to the value stored to c, 20. That's all.

I have one confusion. I read that ternary operators are RIGHT ASSOCIATIVE. Does it mean that in the problem the part ((a < c ) ? c : a) should be evaluated first? Please explain this.

According to C# specification, conditional operator:

The conditional operator is right-associative, meaning that operations are grouped from right to left. For example, an expression of the form a ? b : c ? d : e is evaluated as a ? b : (c ? d : e).

Upvotes: 2

M.kazem Akhgary
M.kazem Akhgary

Reputation: 19179

I assume you want a biggest number.

int a = 5, b = 10, c = 20; 
int result = a > b ? a > c ? a : c > b ? c : b : b > c ? b : c;
Console.WriteLine(result);

It is equivalent to the following if-else condition. I have identified each part with ((...))

if (a > b)                  //((a > b ?)) a > c ? a : c > b ? c : b : b > c ? b : c
{
    if (a > c)              //a > b ? ((a > c ?)) a : c > b ? c : b : b > c ? b : c
    {
        result = a;         //a > b ? a > c ? ((a)) : c > b ? c : b : b > c ? b : c
    }
    else if (c > b)         //a > b ? a > c ? a ((: c > b ?)) c : b : b > c ? b : c
    {
        result = c;         //a > b ? a > c ? a : c > b ? ((c)) : b : b > c ? b : c
    }
    else
    {
        result = b;         //a > b ? a > c ? a : c > b ? c ((: b)) : b > c ? b : c
    }
}
else if (b > c)             //a > b ? a > c ? a : c > b ? c : b ((: b > c ?)) b : c
{
   result = b;              //a > b ? a > c ? a : c > b ? c : b : b > c ? ((b)) : c
}
else
{
   result = c;              //a > b ? a > c ? a : c > b ? c : b : b > c ? b ((: c))
}

If you want smallest number just negate >.

int result = a < b ? a < c ? a : c < b ? c : b : b < c ? b : c;

Upvotes: 0

Related Questions