130 likes | 234 Views
ECE 1305 Introduction to Engineering and Computer Programming. Section 10 Boolean Expressions. Boolean Expressions. Boolean expressions are expressions that are either true or false. Comparison Operators such as > (greater than) are used to compare variables and/or numbers. Example:
E N D
ECE 1305Introduction to Engineering and Computer Programming Section 10Boolean Expressions
Boolean Expressions • Boolean expressions are expressions that are either true or false. • Comparison Operators such as > (greater than) are used to compare variables and/or numbers. • Example: (hours > 40) • The parentheses act as a function that returns a logical true or false.
C++ Relational and Equality Operators int kIce = 273; int kTemp = 250; char init = ’J’; double pressure = 37.2; double MAX_PRESSURE = 38.5; (pressure < MAX_PRESSURE) true (kTemp <= kIce) true (init > ’K’) false (250 >= kTemp) true
C++ Relational and Equality Operators int kIce = 273; int kTemp = 250; char init = ’J’; double pressure = 37.2; double MAX_PRESSURE = 38.5; (init == ’Q’) false (kIce != kTemp) true (kTemp + 30 > kIce) true (kTemp % 2 == 1) false
Logical Operators • More complicated logical statements may be formed using the AND operator (&&) or the OR operator (||) or the NOT operator ( ! ).
Logical Operators • AND Operator (&&)
Logical Operators • OR Operator (||)
Logical Operators • NOT Operator (!)
C++ Relational and Equality Operators int kIce = 273; int kTemp = 250; char init = ’J’; double pressure = 37.2; double MAX_PRESSURE = 38.5; (kIce > 250 && kIce < 280) true (pressure > 40 || init == ’J’) true !(init > ’K’) true (250 >= kTemp && kIce != 273) false (kIce != 273 && 250 >= kTemp) false (short-circuit evaluation)
Boolean Data Type int age = 50; int yearsService = 30; bool eleigible; eligible = (age + yearsService) > 85; assigns the variable eligible the value false
DeMorgan’s Theorem • bool A; • bool B; • !(A && B) = !A || !B • !(A || B) = !A && !B