READING Chapter 5 Section 2 - boolean expressions - arithmetic operators - relational operators - conditional operators - boolean operators - takes boolean values as its operands and returns a boolean value - examples - AND - syntax: && - true if both operands are true - OR - syntzx: || - true if at least one operand is true - NOT - syntax: ! - true if the operand is false - reversing a relational operator means negating the boolean expression - DeMorgan's Law - !(P && Q) <==> !P || !Q - !(P || Q) <==> !P && !Q - arithmetic exception - divide-by-zero error - short-circuit evaluation - for the || operator, if the left operand is true, the right operand is not evaluated - for the && operator, if the left operand is false, the right operand is not evaluated - operator precedence rules required for evaluating boolean expression - ! is considered a unary operator - relational operators come after additive operators - equality operators come after relational operators - && and || come after equality operators - operators with the same precedence are evaluated left-to-right - | and & operations - same thing as || and &&, but doesn't short circuit - always use || and && in this course - cannot do: 80 <= x < 90 - type mismatch - two values of data type boolean - true - false - boolean expression evaluates to one of these - boolean flag tracks program settings or user preferences as a boolean - don't need to directly compare a boolean to true or false, since already evaluates to one or the other - choose identifiers that make it clear what the true and false values mean Quick Check 1. a. true b. true && (false || false) <==> true && false <==> false c. ? && !(false) || ? <==> ? && true || ? <==> true || ? <==> true d. x < y || false <==> x < y 2. a. This actually assigns the value of y to both x and done. What it probably means to do is assign whether x and y are equal to done; this would look like: done = x == y; b. According to precedence rules, first evaluate the parentheses (3 < 5) <==> true: 2 < & && true + 1 == 3 Next, evaulate the arithmetic operation true + 1... this is a type mismatch error: what does it mean to add a boolean and an integer? c. The third line is not a valid statement; here the equality operator is probably being confused with the assignment operator. It should look like: quit = ( 34 == 20 ) && quit; // quit = true