Need explanation of Boolean operators &&, ||, and!


#1



Hello,forum, I need explanation of this lesson, I don't understand at all, how is it work and what is the significance of evaluated there?

https://www.codecademy.com/courses/learn-java/lessons/conditionals-control-flow/exercises/boolean-operators-precedence?action=lesson_resume


System.out.println( !(false) || true && false);

The example above will print out true. In order, the expression is evaluated as follows:
First, the ! Boolean operator in !(false) returns true.
Second, true && false evaluates to false.
Finally, the remaining expression true || false evaluates to true.


I really can't understand it


#2

! will reverse the condition, so !(true) is false and !(false) is true

&& is the and operator, which evaluate the following way:

true && true # true
false && true # false
false && false # false

so only if both conditions are true, the and operator is evaluated to true

the or operator evaluates the following way:

true || true # true 
true || false # true 
false || false # false

so only if both conditions are false, the or operator evaluate to false

this kind of operations are very useful for many things, a simply text based adventure game for example. We can prompt the user for input twice, for example prompt the user if they want to fight, and then if they are strong, if both are true (&&) the user wins the fight (just an example)


The three Boolean operators &&, ||, and!
#3

Thanks for explain, it's easier than what I think, I was confused lol:laughing:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.