Hack Forums
[C] is this explanation of ternary operator valid? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] is this explanation of ternary operator valid? (/showthread.php?tid=5947311)



is this explanation of ternary operator valid? - F.A.S.T - Mar 4, 2019

code (Click to View)

Hi, I want to know if this explanation of the ternary Operator. Using the Code above, Is valid or not.
Here goes;
Code
var = (condition) ? set value if condition one : set value of condition two;

A Variable with a Condition. If the condition is something than the Value of the Variable will be something. But if it's not the value will be different. Basically assign a Variable with a value based on a condition. Is this explanation valid? I need to know this.

Also, Is there any alternative of the Following python code in C++? (For this currently I am using strings)
array with different data types (Click to View)


RE: is this explanation of ternary operator valid? - Europa - Mar 4, 2019

I'd say "return value if condition...." or "result of ternary expression if condition..." instead of "set," but that sounds right. When you plugged in values and tested it, did you find that your explanation was correct?

Another way to explain it is like this:

Code
bool users = (entries > 10) ? true : false; is bool users = entries > 10;



RE: is this explanation of ternary operator valid? - Disease - Mar 4, 2019

(Mar 4, 2019 02:58 PM)F.A.S.T Wrote:  code (Click to View)

Hi, I want to know if this explanation of the ternary Operator. Using the Code above, Is valid or not.
Here goes;
Code
var = (condition) ? set value if condition one : set value of condition two;

A Variable with a Condition. If the condition is something than the Value of the Variable will be something. But if it's not the value will be different. Basically assign a Variable with a value based on a condition. Is this explanation valid? I need to know this.

Also, Is there any alternative of the Following python code in C++? (For this currently I am using strings)
array with different data types (Click to View)

The ternary operator can be thought of as an inline conditional expression. If the conditional validates as true, the first value is returned. Otherwise, the second value will be returned.

At least that's the most concise definition I can personally come up with.

As for your second question, what are you achieving with that Python code? You can't even run that in Python... it'll result in a TypeError if you're actually trying to use the + operator on those different data types.


RE: is this explanation of ternary operator valid? - lockhead - Mar 5, 2019

(Mar 4, 2019 02:58 PM)F.A.S.T Wrote:  Also, Is there any alternative of the Following python code in C++? (For this currently I am using strings)
Code
var = int + "string" + float + bool

This will give you a TypeError. I think what you're trying to do is store varibles of different data types in the same array or list?
Code
var = [1, "hello", 3.14, True]

In C++ you can use std:tuple
https://en.cppreference.com/w/cpp/utility/tuple


RE: is this explanation of ternary operator valid? - Europa - Mar 5, 2019

I looked closer to your code and just want to say that explanation is not for the ternary operator, but for expression that assigns to a variable result of ternary operator. Ternary operator itself is more than that:

Code
int a = 0, b = 0;

condition ? a = 1 : b = 2; // based on condition assign 1 to a or 2 to b
( condition ? a : b ) = 100; // based on condition assign 100 to a or b

You then use the result to assign to a variable, so your explanation is not for ternary operator but for whole expression that uses ternary:

Code
var = condition ? value1 : value2;

You may think this is neat picking but if somebody would read your explanation he may think that assignment to a variable is part of the ternary operator itself, but that is not the case. For example:

Code
function_call( condition ? value_on_true : value_on_false );

Also this at least confusing: "If the condition is something" it should be "if the condition is true then first otherwise (condition is false) the second".

Hopefully that makes more sense. I don't understand your python code like others said it'll just result in a runtime error.


RE: is this explanation of ternary operator valid? - you_can_never_hack_me - Mar 6, 2019

Code
int func (int i, int j) {
  return ((((i != j) ? (i>j ? (--i) : (--j)) : (i-j)) != 0) ? (func (i, j)) : (0));
}



RE: is this explanation of ternary operator valid? - 0xDEAD10CC - Mar 7, 2019

(Mar 5, 2019 03:27 AM)lockhead Wrote:  This will give you a TypeError. I think what you're trying to do is store varibles of different data types in the same array or list?
Code
var = [1, "hello", 3.14, True]

In C++ you can use std:tuple
https://en.cppreference.com/w/cpp/utility/tuple

Only if the size of the container needs to remain the same, otherwise it wouldn't work. You'd have to use one of the C++ types that handle things by void pointers internally or use a proxy type of some sort like boost::any.


RE: is this explanation of ternary operator valid? - 6577657534735 - Mar 7, 2019

1 means nothing
"hello" means nothing
3.14 means nothing without context
True means nothing

Put this in a struct with named fields, not a tuple or any other utility


RE: is this explanation of ternary operator valid? - lockhead - Mar 8, 2019

(Mar 7, 2019 11:06 PM)Abraluna Wrote:  1 means nothing
"hello" means nothing
3.14 means nothing without context
True means nothing

Put this in a struct with named fields, not a tuple or any other utility

I'm not here to give your life meaning buddy.
I gave an example of code that would run, because OPs code neither makes sense nor runs. And yes, I did use random values to represent
the types. You got me.

But yes use a struct when you're doing this in C++, I was just talking about Python.


RE: is this explanation of ternary operator valid? - F.A.S.T - Mar 8, 2019

That Python code I wrote was just an example of what I was trying to ask. But lets just forget it.

Thanks I lot I finally understood what a Ternary Operator is.