programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Having a hard time understanding Tenary operators

Naziru Gelajo
Ranch Hand
Posts: 175
1
I'm working on a homework assignment and having a problem with these questions:

1. Logic. Type an expression that is equivalent to, but much simpler than, the given expression.

!(!B && (A || B)) ___________________

(A || B) && (!A || B) ___________________

(A && B) || A ___________________

(C || B || A) && B ___________________

!( !B || !( A && B ) || !A) ___________________

2. Conditional operator. Type an equivalent expression not using the conditional operator. (You can use &&, || and ! ) Make your answer as simple as possible.

(!A && !B) ? false : true ___________________

A ? B ? C : false: true ___________________

A ? B : A ___________________

A ? B : C ? A : B ___________________

I am not 100% sure how it works, so if someone can please shed some light, it would be greatly appreciated. Thanks!

Piet Souris
Master Rancher
Posts: 2044
75
Hi Naziru,

well, it all boils down to two things:

- knowing your basic formulae like: ! (A && B) = !A || !B, A || (B && C) = ..., et cetera
- gaining enough experience manipulating those expressions

How do you rank yourself?

Shaun Wild
Greenhorn
Posts: 8
I won't delve into helping you solve your boolean algebra problem however I can link you this useful resource:

As for your Ternary operators, think of them as shorthand functions to take a boolean and output one of two values depending on whether the boolean is true or not. For example:

You can also use expressions

You can also chain multiple together (However I don't recommend doing this as it seriously hurts the readability of your code.

Hope I helped shed some light on that for you

Liutauras Vilda
Sheriff
Posts: 4923
334
Have a look at the table in this article (<-- link), scroll down around to the middle. You may find this useful too.

Liutauras Vilda
Sheriff
Posts: 4923
334
Naziru Gelajo wrote:(A && B) || A
For instance look at this expression.

If you're not familiar with those propositional laws, you can try to think in practical way by manipulating with actual values and see how you can simplify.

(A && B) || A

1. Lets assume A is true. If A is true, then this expression always will be true no matter what B value is, because those two expressions (A && B) || A are connected with logical OR, so basically from left side of expression you're care of A only.
2. Now think the same way if A is false. If A is false, left expression will be always false, no matter what B is, and indeed right side will be false, so false OR false will be false.
So, what you can conclude from this is? That no matter what B is, you rely always on A value.

Stephan van Hulst
Saloon Keeper
Posts: 7992
143
As for the ternary operator, you can substitute with propositional calculus like this:

Replace:with:
X ? Y : false X && Y
X ? Y : true!X || Y

Campbell Ritchie
Marshal
Posts: 56570
172
Naziru Gelajo wrote:. . . .
(!A && !B) ? false : true ___________________

A ? B ? C : false: true ___________________
. . .
Those are the only two expressions actually containing the ternary operator (officially called the conditional operator. If you look at that link, you will find ?: associates to the right. You may also find the link hard to read. That means you need to parse it as
A ? B ? C : false: true
or
A ? (B ? C : false): true
Note that expression contains two ?s and two :s, so it is two expressions nested inside each other. It would take the same process to parse if it had three ?s and three :s
SW is right: you need to find the normal rules of Boolean algebra to reduce those expressions.

Winston Gutkowski
Bartender
Posts: 10575
66
Naziru Gelajo wrote:I'm working on a homework assignment and having a problem with these questions:

OK, well since your question is about ternary operators (actually called a "conditional" operator in the Java Language Specification), let me try this.

A ternary operator takes the form:
x ? y : z
where:
• x is a boolean expression.
• y and z are values (or expressions that yield) the same type.
• In your case, they're all booleans, which unfortunately makes them trickier to read, because very often the only way you can tell a boolean value from a boolean expression in Java is by where it is.

And the best way (I find) to think of them is as:where '[return]' simply means "have the expression return" - Java doesn't actually have such a verb, since that's what expressions do anyway.
Indeed, in some languages, that is exactly how they're written (without the '[return]').

The only wrinkle is that your last question
A ? B : C ? A : B
contains a "stacked" ternary expression. This is perfectly valid, since z is allowed to be an expression, so you can think of it as:
My advice: Take one question at a time, and break it down on paper. Hopefully, the stuff above may help you do that.

Winston

Piet Souris
Master Rancher
Posts: 2044
75
Had OP sent in some piece of code, with the remark "I'm having problems, can anyone help", we all would have reacted with
"tell us where you are stuck".

This assignment is in all likelyhood based on material that OP must have had. But now we all start explaining about

I wonder what the difference is. The pitfall. as I see it, is that we end up in a very lengthy topic, like the one about
logic gates, and like other ones in the past.

Stephan van Hulst
Saloon Keeper
Posts: 7992
143
Campbell Ritchie wrote:If you look at that link, you will find ?: associates to the right. [...] That means you need to parse it as
A ? B ? C : false: true

Being pedantic here, this hasn't to do with associativity, since there's only one way to parse that expression. Associativity only comes into play in expressions of the form A ? B : C ? D : E.

Campbell Ritchie
Marshal
Posts: 56570
172
But that is an expression of that form. Only in the case shown by OP the subexpression is between the ? and the : whereas Stefan's example shows the subexpression after the :

Stephan van Hulst
Saloon Keeper
Posts: 7992
143
Sorry, maybe this is more clear:

??:: Can only be parsed one way, regardless of associativity: ?(?:):

?:?: Can be parsed as (?:)?: or as ?:(?:)

God forbid the left associative version, I'm looking at you PHP >:[

Campbell Ritchie
Marshal
Posts: 56570
172
Yes, that is clearer . The earlier expression using ?: was in the form ?:‍(?:‍)

Campbell Ritchie
Marshal
Posts: 56570
172
• 1
Winston Gutkowski wrote:. . .. . .
You are allowed to have ?: after return. Example, which is probably how this is implemented:-. . . but your first example can also be thought of like this, as an expansion of
result = x ? y : z;

Winston Gutkowski
Bartender
Posts: 10575
66
Campbell Ritchie wrote:but your first example can also be thought of like this, as an expansion of

Yeah, that's probably better. Cheers.

Winston

Naziru Gelajo
Ranch Hand
Posts: 175
1
Thanks everyone, this helps a lot.

Naziru Gelajo
Ranch Hand
Posts: 175
1
See I'm still not understanding how something like X ? Y : false is the same as X && Y, or (X ? Y : true) is the same as (!X || Y). From my understanding, I always thought that something like

X ? Y : False means "if X is true then Y else False" Sorry if I'm feeling dumb.

Stefan Evans
Bartender
Posts: 1837
10
It might help to write it out as if statements or tables to see the value on every path

Like this:

X ? Y : true;

is effectively

X:true - Y:true --> true
X:true - Y:false --> false
X:false - Y:true --> false
X:false - Y:false --> false

Can you now see how it gets to X && Y?

Naziru Gelajo
Ranch Hand
Posts: 175
1
It might help to write it out as if statements or tables to see the value on every path

Like this:

X ? Y : true;

is effectively

X:true - Y:true --> true
X:true - Y:false --> false
X:false - Y:true --> false
X:false - Y:false --> false

Can you now see how it gets to X && Y?

You have a right to kindly call me stupid, because I still don't understand. In regards to Computer Science, Ternary Operators is something that I have never understood 100%. I kind of get the concept behind it, I also know this sounds embarrassing, but I have never really used them in my code before when I write code.

Junilu Lacar
Sheriff
Posts: 11494
180
X ? Y : true

You have to break this down into two parts:

Part 1: X ? Y : true

When X is true, return whatever Y is

X is true, Y is false --> return false
X is true, Y is true --> return true

This is the bottom half of the X && Y truth table

XYX && Y
falsefalsefalse
falsetruefalse
truefalsefalse
truetruetrue

Part 2: X ? Y : true

X is false --> return true

This is half of negation - !X

I'll leave it up to you to follow the logic for combining Part 1 and Part 2. Just start by figuring out the answers to these questions: "What happens when X is true? What happens when X is false?"

Winston Gutkowski
Bartender
Posts: 10575
66
Naziru Gelajo wrote:See I'm still not understanding how something like X ? Y : false is the same as X && Y, or (X ? Y : true) is the same as (!X || Y). From my understanding, I always thought that something like
X ? Y : False means "if X is true then Y else False" Sorry if I'm feeling dumb.

No, you're not. In fact, you expanded the statement absolutely correctly.

X ? Y : false means precisely "if X is true then (the result is) Y; else (the result is) false".

So now look at
X && Y
What does that do?

Well first of all: it means absolutely nothing unless both X and Y are booleans - '2 && 7' is nonsensical. So you know that both X and Y are either true or false.

So now you can create truth tables as you've been shown, or you can simply work it out deductively:
'X && Y' means "if both X and Y are true, (the result is) true; otherwise (the result is) false"
so:
1. if X is false, what does X && Y return? (probably the easiest)
2. if X is true, what does X && Y return? (you have two possibilities)

And if you now put those results back into your first statement, you'll work out that they are identical.

Try it.

And then do the same thing for 'X ? Y : true'.

It's very important that you understand this stuff, so take time on it, because it doesn't come naturally to everyone.

It certainly didn't to me when I was starting out; but gradually, after writing things out in longhand (or generating truth tables) dozens of times, I gradually got familiar with it. But even now, after nearly 40 years as a programmer, if I get a complicated expression I STILL have to write it out.

And you know what? I think perhaps that makes me a better programmer, because I'll avoid writing complex expressions if I possibly can; whereas someone who can "see" them instinctively might not realise the confusion they cause.

HIH

Winston

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.