# How does the Conditional (ternary) Operator get evaluated? [closed]

in all of the examples I've seen how this works

`````` condition?value = true:value = false
``````

I have yet to find how it actually works. taking this mode of operation it is almost plainly explained.

``````Short-circuit evaluation

As logical expressions are evaluated left to right, they are tested
for possible "short-circuit" evaluation using the following rules:

false && anything is short-circuit evaluated to false.
true || anything is short-circuit evaluated to true.
``````

What actually is meant by the term Short-circuit ?

and does the ?: come to the same conclusion on the left side of the ? just like the && and || does so that the right side of the ? short-circuits (fails) so then it goes to the right of the : then it does not fail. instead it completes an action.

The term short-circuit confuses me because I do not have a clear understanding it the term.

all I do know is this.

that this works on a return value of zero or true or yes

``````if true && then do this
``````

if the return value on the left side is a non-zero it will fail

``````if false || then do this
``````

if the return value on the left side is zero it will fail

in that understanding I then pose the question again.

Does the Conditional (ternary) Operator get evaluated in the same manner and operates in the same manner?

I have to brake it apart to demonstrate this question.

``````false ? anything is short-circuit evaluated to false.
true : anything is short-circuit evaluated to true.
``````

it looks like it is being explained as a

`````` !true ? then fail
true : then fail
``````

or

``````  true ? then no failure
false : then no failure
``````

it draws the conclusion on the very far left of the ?:

`````` hypothesis ? conclusion true : conclusion false
``````

in the same manner for the Logical operators

`````` hypothesis &&  conclusion true operation preformed

hypothesis || conclusion false operation preformed
``````

do they both operate on the same methodology?

the two Logical operators seen as one and the Conditional (ternary) Operator

are all three Logical operators?

I am tagging them in all of the programming languages that I know they all can be used in so I can get a over all truth to this question(s).

I do pray that I have not confused anyone on this. it yes then it is probably due to my confusion on it.

## closed as off-topic by Gilles, Julie Pelletier, G-Man, Michael Homer, Satō KatsuraJan 21 '17 at 12:38

• This question does not appear to be about Unix or Linux within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

• I'm voting to close this question as off-topic because it is a C programming question at heart but too confused to migrate to SO. – Michael Homer Jan 21 '17 at 6:31
• @MichaelHomer closing this thread because why? logical operations confuse you if it is not in C programming. do you not have an understanding that in every programming lang this logic gets used therefore it Qualifies in all programming langs. I am closing you for abuse of power due to your lack of ability to understand how Logic can be used in other programming lang other then just C programming as well in life in general. causing you to make faulty judgment calls. you are an unjust judge and need to have your authority stripped off of you until further notice. – uxserx-bw Jan 21 '17 at 14:40
• @MichaelHomer educate yourself like I am trying to do. Look up BASH Logical Operators tldp.org/LDP/abs/html/ops.html – uxserx-bw Jan 21 '17 at 14:51
• For what it's worth: I voted to close your question as too broad. It would take at least a book chapter to cover everything you ask above. Then I downvoted your post, for your attitude towards @MichaelHomer. – Satō Katsura Jan 21 '17 at 17:07
• Bash doesn't have a ternary operator. – Michael Homer Jan 21 '17 at 18:23

By 'short-circuit' they mean that if you had an expression like:

``````( something evaluating to false) && (something else)
``````

It would never execute anything in the `(something else)` - it would immediately terminate because `false && anything` will always be false.

Similarly:

``````(something evaluating to true ) || (something else)
``````

There is no point in evaluating the `(something else)` because the condition is already true.

As for the ternary operator `A ? b : c` Only `A` is a boolean expression. Based on its value either `b` or `c` will be evaluated (`b` if `A` is true, otherwise `c`). So there is no "short-circuiting" going on here like there is with the `&&` and `||` operators. There is never a condition in which both `b` and `c` will be executed, it will always be one or the other.

It may help to think of the ternary as an if-else statement. In the example above, `A` is the conditional-expression, `b` is the `then` clause and `c` is the `else` clause. The only difference is that the entire ternary expression will result in the value of `b` or `c`, depending on whether `A` is true or false.

Here's an example in C. Say I want a string stating whether a number `N` is even or odd:

``````char *result = ((N % 2) == 0) ? "even" : "odd";
``````

Here's a way to do it with "short-circuiting" (not good style, but it demonstrates the concept):

``````char *result = "even";
((N % 2) == 0) || (result = "odd");
``````

In this last example, if `N` is even it will short-circuit the assignment to "odd" - the assignment will never occur because the expression will not be evaluated since the result is already known to be true by the time it reaches the `||`, so result will remain "even".

"Short-circuit" means that (going in the evaluation order) the moment the result is clear all further evaluation is skipped.

For `||` and `&&` this means that in `x = (true || send_mail())` the call to `send_mail()` is skipped because the operands are evaluated from left to right and true or'd with anything results in true.

For the ternary operator the term "short circuit" can be used in a broader sense, as there is no left-to-right evaluation order as for the logical operators. The concept here is more precisely called "lazy evaluation". This simply means that, after evaluating the condition, only the matching "conclusion" part is evaluated.

So `x = (ask_user() ? try_send_mail() : try_send_letter())` only executes `try_send_mail()` or `try_send_letter()` but never both at once.

• I would not call it "short-circuiting" for the ternary operator any more than you "short-circuit" an `else` clause when the `if` condition is true. The ternary operator is really just an if-else construct collapsed into a single line, except that it can return a value. – BobDoolittle Jan 21 '17 at 0:05
• @BobDoolittle You're right, but the concept is similar. x = a && b && c could also be rewritten as multiple if-constructs. I edited my answer to refer to lazy evaluation. – cg909 Jan 21 '17 at 0:23
• To me "short-circuit" implies that it could evaluate the entire expression, but it stops prematurely in some circumstance. That's not the case with the ternary. It will always evaluate one and only one of the RHS expressions. I suppose other interpretations are possible since it's not exactly a technical term :-) – BobDoolittle Jan 21 '17 at 0:43
• Sort-circuiting applies to `evaluations` of logic statements. Short-circuit can still apply to the condition in a ternary operator as the condition can be compound. In the ternary `operator`, the `?` and `:` are part of the syntax, not the condition. Probably poorly stated, but the idea still holds. – Gypsy Spellweaver Jan 21 '17 at 3:18
• @GypsySpellweaver is it then correct then to say when the logic short-circuits is equivalent to a fail state. or causes a failure that is why it will not work. Is what I said a proper logical thought (as well)? – uxserx-bw Jan 21 '17 at 17:25