Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with understanding Logical Operators  RSS feed

 
Lisa Austin
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working through CodingBat.com java practice problems and I don't understand Logical Operators.


Using this format I got back and "True" and a "False" when I expected a "True" and a "True".



This returned "True" and "False" as well



The statements in the below code returned "True" and a "True" which is what I expected.
Could someone please help me understand what the difference between (aSmile && bSmile == false) and (aSmile == false) && (bSmile == false) ? I read (aSmile && bSmile == false) as "aSmile and bSmile equals "False") but what does it really say?

Thank You


 
Stephan van Hulst
Saloon Keeper
Posts: 7804
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lisa,

We can't really help you if we don't know what the values of aSmile and bSmile are. What were their values so that the method returned true, and what were their values so that the method returned false?
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never write == true and == false.
Never b == true. Just write b.
Never b == false. Write !b.
The use of == is not only poor style, but also error‑prone. Every now and then we see somebody write = instead by mistake.

p && q uses the standard truth table for AND, but is a short-circuit operator. If the left value is false, the whole expression will be false and the right value is not evaluated any more.
pqp && q
FFF
FTF
TFF
TTT
Note you only get T if both operands evaluate to true. && has a higher precedence than ||. They are not called logical operators but conditional operators, despite what it says in the Java Tutorials.
Also, the old Sun style guide ยง10.5.2 tells you, don't writeSimply write
return aSmile && bSmile;Your conditions for the if-else are redundant; if you get to line 2 there is only one option available. Your second code block however reduces to
return true;
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you come across the ^ operator? Or the equality operators? You can write
if (p == q)...
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The real problem with what you expect to get might be that you don't understand the operator precedence. For example,
is the same as not
- another reason to never check a Boolean against true or false.
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done noticing that problem FS FK

[edit]Sorry for wrong initials[/edit]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!