Boolean Logic and Relational Operators

Boolean Expressions

Part of: Algorithms, Iteration & Simulation

Before a program can make a decision, it needs a way to ask a yes-or-no question. The answer to such a question is a Boolean value: exactly one of True or False. Every loop that stops, every if that fires, and every search that succeeds rests on Boolean logic. Relational operators A relational operator compares two values and produces a Boolean: - == equal to (note: two equals signs, not one) - != not equal to - <, , <=, = the usual orderings Watch the difference between = (assignment, stores a value) and == (comparison, asks a question). Mixing them up is the most common Boolean bug. Logical operators You combine Boolean values with three logical operators : - and, True only if both sides are True - or, True if at least one side is True - not, flips True to False and vice versa The AP exam uses the words AND, OR, NOT in pseudocode; the meaning is identical. A handy reference is the truth table for and: only True and True yields True; every other combination is False. For or, only False or False yields False. Short-circuit evaluation Python stops as soon as the answer is known. In a and b, if a is False the whole expression is False and b is never checked. This short-circuit behavi

Challenge: Both Positive?