Boolean is false but it should be true?


#1

# boolean_2 = false || -10 > -9
#boolean_2 = false

I’m not understanding why it’s false, shouldn’t it be true since the Boolean_2 variable is false and/or false?

doesn’t false=false make it true?

or is it just because the boolean_2 variable is false but it’s also false on the other side of the “||”?? so it’s an AND not an OR???

AND this one:

# boolean_2 = !true && (!true || 100 != 5**2)
#boolean_2 = false

isn’t boolean_2 [not true] AND ([not true] OR [100 Not equal to 5*5])?

#boolean_3 = true || !(true || false)
#boolean_3 = true

and boolean_3 [true] OR ([not true OR not false])

I had those reversed, but I’m guessing I’m not getting the OR command, is the OR and If/Then/But instance?

just wanted to make sure I’m understanding this…thanks!


#2

false == false is true, but false || false is false.

Operator, actually. OR short-circuits on true, so any number of operands can be false, but if any one of them is true, the outcome is true.

t || f || f || f || f  => true

AND short-circuits on false, so the minute we see false &&... we can immediately ignore the rest of the expression. It is false on that evidence alone, just as OR yields true on a single true.

AND, and OR are binary operators in that they take two operands. NOT is a unary operator that always precedes it value. In this case we use the term value because it can be anything, not just a boolean.

! 1 => false
! '' => true

The operator not only negates its operand, it casts it to a boolean before negating.

Once again we see the above is immediately true since the principal operator is OR. We may ignore the remaining operand. Seeing these sorts of things on sight is a very important skill to develop, along with the thought process involved in making quick assessments.

Before letting the computer tell you what it sees, be sure to pencil sketch for yourself, and eventually sketch in your mind the outcome. This is not a memory based skill, but one which must be practiced to acquire any degree of proficiency.


#3

Extended example

You might find this an interesting parallel, albeit involving the physics of electricity. The logic is directly parallel with program logic–electric current and voltage presence versus program flow and state inspection.

logic gate (AND, OR, XOR, NOT, NAND, NOR and XNOR)

It’s definitely something to put on the back burner while you progress, but which you might find rewarding when you come back to review.


#4

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