Understanding Boolean Expressions and Operators in C++ Programming
130 likes | 243 Vues
This course segment introduces Boolean expressions, which evaluate to true or false in C++ programming. It covers the use of comparison operators like greater than (>) and demonstrates examples to illustrate how these expressions work in determining truth values. Furthermore, it delves into relational and equality operators, including AND (&&), OR (||), and NOT (!), explaining how to form complex logical statements. Lastly, it discusses the precedence of operators and DeMorgan’s theorem, essential for effective programming logic and condition evaluations.
Understanding Boolean Expressions and Operators in C++ Programming
E N D
Presentation Transcript
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