1 / 13

Understanding Boolean Expressions and Operators in C++ Programming

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.

Télécharger la présentation

Understanding Boolean Expressions and Operators in C++ Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ECE 1305Introduction to Engineering and Computer Programming Section 10Boolean Expressions

  2. 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.

  3. Relational and Equality Operators

  4. 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

  5. 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

  6. Logical Operators • More complicated logical statements may be formed using the AND operator (&&) or the OR operator (||) or the NOT operator ( ! ).

  7. Logical Operators • AND Operator (&&)

  8. Logical Operators • OR Operator (||)

  9. Logical Operators • NOT Operator (!)

  10. 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)

  11. Precedence of Operators

  12. Boolean Data Type int age = 50; int yearsService = 30; bool eleigible; eligible = (age + yearsService) > 85; assigns the variable eligible the value false

  13. DeMorgan’s Theorem • bool A; • bool B; • !(A && B) = !A || !B • !(A || B) = !A && !B

More Related