# question on expressions

phillippa Jame

Greenhorn

Posts: 2

posted 4 years ago

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.

Of the following examples, which are considered logical expressions?

1.

2.

3.

Thanks if anyone could explain if there is a difference.

posted 4 years ago

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?

That's a boolean expression in Java

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

In there,

Difference between what and what?

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?

phillippa Jame

Greenhorn

Posts: 2

posted 4 years ago

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 &&.

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 &&.

posted 4 years ago

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

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

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

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

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)

!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

posted 4 years ago

You're right. My bad. Should have said something like:

If X, Y, and Z are all logical expressions, then so is

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 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

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.

Campbell Ritchie

Sheriff

Posts: 50647

83

Campbell Ritchie

Sheriff

Posts: 50647

83

posted 4 years ago

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

Two logical expressions can be joined by logical operators, of which the best-known are ∨ ∧ ¬ ⇒ ⊕ and ⇔; their Java™ equivalents are || && !

So i > j is logical and i > j ∧ i > k is logical too. Just as this is logical (two propositions joined by “and”):

The following is a question, and therefore not a proposition and not a logical expression:

The following is not a logical proposition, but an instruction:

Therefore, an instruction constrained by a logical expression remains an instruction. So,. . . is not

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

Forget all about Java™ for the time being. A logical expression is one which can be translated into ordinary English like this

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!It is raining.

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 operatorsSo 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:

[But the answer, yes or not, in that contextIs it raining?

**is**a logical proposition.]The following is not a logical proposition, but an instruction:

. . . although an explanation like “because it is raining” contains a logical proposition.Come and play indoors.

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`.