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!

http://www.allaboutcircuits.com/textbook/digital/chpt-7/boolean-rules-for-simplification/

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

For instance look at this expression.Naziru Gelajo wrote:(A && B) || A

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.

Replace: | with: |
---|---|

X ? Y : false | X && Y |

X ? Y : true | !X || Y |

*The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.*

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 asNaziru Gelajo wrote:. . . .

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

A ? B ? C : false: true ___________________

. . .

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.

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.

`boolean`s, 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

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

"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

boolean algebra, pointing towards courses about this and explaing ternary operators.

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.

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__*The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.*

**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 >:[

*The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.*

- 1

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 ofWinston Gutkowski wrote:. . .. . .

`result = x ? y : z;`

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

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

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?

Stefan Evans wrote:I had to think about this one for a minute too.

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.

You have to break this down into two parts:

Part 1:

**: true**

__X ? Y__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

X | Y | X && Y |
---|---|---|

false | false | false |

false | true | false |

true | false | false |

true | true | true |

Part 2:

**? Y**

__X__

__: 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?"

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

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

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

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