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?
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, found is a boolean expresion.
Thanks if anyone could explain if there is a difference.
Zeeshan Sheikh wrote: . . . So all of your examples are logical expressions.
No, you are mistaken there.
Joined: Oct 13, 2005
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.