Jump to content

What the heck the real definition of Ternary Operator!?

Yukha

I just learned about unary,binary, ans ternary operator..

 

From what i learned, ternary is operation that needs 3 operand.. 

 

So, why conditional if else ?: is ternary!! :/ 

How about a = b+c+d; ? Aren't they ternary too? (B+c+d)

 

please help:/ im dying from this shit

 

Link to comment
Share on other sites

Link to post
Share on other sites

ternary  = composed of three items.

 

Operation which involves three variables or in your case  variable =  (condition is true) then one thing else another thing.   a = (1>0) ? true : false;

 

a = b + c+ d  is not, as b + c + d can be simplified to a single variable. It's two additions .. add c to b, then add d to b , then put b in a  ... they're simple operations.

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 2017-10-20 at 10:11 PM, mariushm said:

ternary  = composed of three items.

 

Operation which involves three variables or in your case  variable =  (condition is true) then one thing else another thing.   a = (1>0) ? true : false;

 

a = b + c+ d  is not, as b + c + d can be simplified to a single variable. It's two additions .. add c to b, then add d to b , then put b in a  ... they're simple operations.

 

Just to expand this a bit, what we're talking about is called the arity of the function. https://en.wikipedia.org/wiki/Arity

It should be noted that all infix notation can be rewritten in prefix (or, Polish) notation, examine this C++ pseudo code as an example

int ternary(bool cond, int tRes, int fRes) {
	if (cond) return tRes;
	return fRes;
}

Now, with this given code we can write these two equivalent statements

a = (a > 0) ? a : b;

a = ternary(a > 0, a, b);

And as you can see the named function has 3 parameters, and has an arity of 3 (AKA, is a ternary function). 

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
Share on other sites

Link to post
Share on other sites

On 10/21/2017 at 4:11 AM, mariushm said:

a = b + c+ d  is not, as b + c + d can be simplified to a single variable. It's two additions .. add c to b, then add d to b , then put b in a  ... they're simple operations.

Actually, c and b are added together but the result is stored in a temporary rvalue which is then added to d. b is never modified as your answer implies.

Link to comment
Share on other sites

Link to post
Share on other sites

On 10/20/2017 at 7:03 PM, Yukha said:

I just learned about unary,binary, ans ternary operator..

 

From what i learned, ternary is operation that needs 3 operand.. 

 

So, why conditional if else ?: is ternary!! :/ 

How about a = b+c+d; ? Aren't they ternary too? (B+c+d)

 

please help:/ im dying from this shit

 

While you're correct that b + c + d is ternary in the sense there are three operands, in computer science, a ternary operator requires three distinct arguments. b + c + d by itself can be condensed into a single distinct argument. If for example this was b = 1, c = 2, and d = 3, then b + c + d is the same as 1 + 2 + 3 which is the same as 6. There's no difference if you did a = 6 or a = 1 + 2 + 3.

 

a = b ? c : d is shorthand for

if (b)
  	a = c
else
  	a = d

And hence you need three distinct arguments.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×