Help 12. Combining Boolean Operators PLEASE


#1




I got the answer right, but I really do not understand the concept (now that the brackets are involved and we are combining boolean operators). If "&&" means everything has to be true in ordered to be labeled "True", why is the code below considered true?:

(3 < 4 || false) && (false || true).

If I consider all the possible combinations here, only some are true:
3 < 4 && false = NOT TRUE
3 < 4 && true = TRUE
false && false = TRUE (I think?)
false && true = NOT TRUE

So how is the right answer: "True" ?! I'm clearly missing something conceptually!

Thank you in advanced :slight_smile: .


# boolean_1 = (3 < 4 || false) && (false || true)
boolean_1 = true


#2

false. Always false.

Starting with the brackets, we get

true    &&    true

which yields true. This is the only configuration of && that will be true. All others are false.


#4

We need to break every expression down to a single value, then cast as boolean. After that precedence takes over.

NOT AND OR

in that order assuming we've handled all the expressions in brackets and reduced their parent expression to a single value. This is not as complicated as it sounds, it just respects that expressions may take any form. Our first role is to reduce those expressions. There may be math, and or logic involved.

T && T  ==  TRUE

T && F  ==  FALSE
F && T  ==  FALSE
F && F  ==  FALSE


T || T  ==  TRUE
T || F  ==  TRUE
F || T  ==  TRUE

F || F  ==  FALSE

#5

Light-bulb went off! Thank you so much... I was writing a long reply that was only confusing me further, but this little chart kicked my brain in order. It makes sense now (to me), as rules of | | are inclusive, only one true is required to make the expression true!

THANK YOU!


#6

This is an area where many learners leap through and chug along. Mistake. Stick with this module for a good week and explore the logic until it's molded in you mind.

def name_cap(s)
    s.capitalize!
end

print "Enter your first name: "
name = gets.chomp

name = name_cap(name) || name

Made you look.

First off, recognize that this is an assignment, so there is something to assign on the right hand side. What it is will fall out of the logic expression.

string.capitalize!

will return nil if string is already capitalized. That is the key in this logic.

If the return value in expression A is nil then the logic reverts to B in the OR expression, so name is allowed to stay as is.


#7

This entire comment confuses me thoroughly, I will work on it and report back!


#8

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