# question on expressions

phillippa Jame
Greenhorn
Posts: 2
Hi I am confused about something I'm working on. I have rewritten the problem to ask for help as I'm not sure how you define a logical expression/expression.

Of the following examples, which are considered logical expressions?

1.

2.

3.

Thanks if anyone could explain if there is a difference.

Jeff Verdegan
Bartender
Posts: 6109
6
phillippa Jame wrote:Hi I am confused about something I'm working on. I have rewritten the problem to ask for help as I'm not sure how you define a logical expression/expression.

Of the following examples, which are considered logical expressions?

I don't think "logical expression" is part of the official Java lexicon. So perhaps you mean "boolean expression"? Or perhaps you are referring to a "logical expression" as defined in some other context?

1.

That's a boolean expression in Java

2.

That's not legal Java. But if you replace the AND with && or &, then yes, it is a boolean expression.

3.

In there, found is a boolean expresion.

Thanks if anyone could explain if there is a difference.

Difference between what and what?

Zeeshan Sheikh
Ranch Hand
Posts: 144
Logical Expression Definition: A logical expression consists of one or more logical operators and logical, numeric, or relational operands. So all of your examples are logical expressions.

phillippa Jame
Greenhorn
Posts: 2
Thanks guys - the way my textbook defined "logical expression" was as "an expression that evaluates to a boolean value"

However throughout the textbook, they only gave examples like: (5>2)

So that's why I wasn't sure if the other examples I created were actually valid "logical expressions" or not.

ETA - Forgot about AND and &&.

Jeff Verdegan
Bartender
Posts: 6109
6
phillippa Jame wrote:Thanks guys - the way my textbook defined "logical expression" was as "an expression that evaluates to a boolean value"

That's about as concise and correct a definition as you're likely to find.

However throughout the textbook, they only gave examples like: (5>2)

And that is an expression that evaluates to a boolean value.

If X and Y are both logical expressions, the so are:

!X
X && Y
X & Y
X || Y
X | Y
X ^ Y
X ? A : B (note that A and B can be of any type)
(X)

And I'm sure there are others I've forgotten

Bear Bibeault
Author and ninkuma
Marshal
Posts: 65274
95
Jeff Verdegan wrote:X ? A : B (note that A and B can be of any type)

This one isn't technically correct unless A and B are booleans, as it will resolve to the type of A and B (which must be of the same type).

Jeff Verdegan
Bartender
Posts: 6109
6
Bear Bibeault wrote:
Jeff Verdegan wrote:X ? A : B (note that A and B can be of any type)

This one isn't technically correct unless A and B are booleans, as it will resolve to the type of A and B (which must be of the same type).

You're right. My bad. Should have said something like:
If X, Y, and Z are all logical expressions, then so is

X ? Y : Z

Long day trying to unravel concurrency problems and my brain is about done for.

@phillippa Jame : Sorry for any confusion I may have caused.

Bear Bibeault
Author and ninkuma
Marshal
Posts: 65274
95
"This is your brain. This is your brain on concurrency."

Campbell Ritchie
Sheriff
Posts: 50647
83
Zeeshan Sheikh wrote: . . . So all of your examples are logical expressions.
No, you are mistaken there.

Campbell Ritchie
Sheriff
Posts: 50647
83
You have not presented Java™ code at all. In which case this thread is a good candidate for moving to “General Computing”. Beware of the moderate there
Forget all about Java™ for the time being. A logical expression is one which can be translated into ordinary English like this
It is raining.
That is a proposition, which can be true or false. If you go out in the fresh air and get wet, then that proposition is probably true!
Two logical expressions can be joined by logical operators, of which the best-known are ∨ ∧ ¬ ⇒ ⊕ and ⇔; their Java™ equivalents are || && ! (p ? q : true) ^ and ==. And every logic book seems to have a different range of operators
So i > j is logical and i > j ∧ i > k is logical too. Just as this is logical (two propositions joined by “and”):
It is raining and I have got my umbrella.

The following is a question, and therefore not a proposition and not a logical expression:
Is it raining?
[But the answer, yes or not, in that context is a logical proposition.]

The following is not a logical proposition, but an instruction:
Come and play indoors.
. . . although an explanation like “because it is raining” contains a logical proposition.

Therefore, an instruction constrained by a logical expression remains an instruction. So,. . . is not per se a logical expression. It might be if foo() and bar() are themselves logical, in which case it reduces to p ∧ foo ∨ ¬p ∧ bar.
So, I disagree about ?: which is rather like compressing an if-else into the middle of a statement. Although ?: takes a logical expression as its left operand, its type is that of the middle and right operands. Something like. . . will display a number, not true/false.