• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question on expressions

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 144
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"This is your brain. This is your brain on concurrency."
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zeeshan Sheikh wrote: . . . So all of your examples are logical expressions.

No, you are mistaken there.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic