LOGICAL OPERATORS: OR, AND

These are used just as in everyday talk. You can say “I want apple OR orange”, or you can write:

string sFruit = “orange”
if sFruit == “apple” OR sFruit == “orange” then print “I’ve got a delicious fruit.”

In other words, the operator OR will return true if either condition on the left or right is true. If the condition on the left is satisfied, it won’t even bother to check the right side.

Operator AND needs “true” variables on both sides to return true, so both sides will be checked.

bool bSunny = true
int iDay = 5
if bSunny AND iDay == 5 then print “It is sunny this Friday.”

For those of you inclined toward electronics, OR operator can be represented as switches connected in parallel while AND works like switches connected in series.

Leave a comment

Your email address will not be published. Required fields are marked *