chap04 - Chapter 4 Control Control Construct Construct 1 Key Concepts logical values and operation true tables bool type relational operators

chap04 - Chapter 4 Control Control Construct Construct 1...

This preview shows page 1 - 16 out of 64 pages.

1 Chapter 4 : Chapter 4 : Control Control Construct Construct
Image of page 1
2 Key Concepts: Key Concepts: break statement enum statement for construct while construct do construct infinite loops invariants logical values and operation true tables bool type relational operators short-circuit evaluation if-else statement switch statement
Image of page 2
Boolean Algebra Boolean Algebra Logical expressions have the one of two values - true or false A rectangle has three sides The instructor has a pleasant smile The branch of mathematics is called Boolean algebra Developed by the British mathematician George Boole in the 19th century Three key logical operators And Or Not
Image of page 3
Boolean Algebra Boolean Algebra Truth tables Lists all combinations of operand values and the result of the operation for each combination P Q P and Q false false false false true false true false false true true true True table of logical “ and
Image of page 4
Boolean Algebra Boolean Algebra Truth tables Lists all combinations of operand values and the result of the operation for each combination P Q P or Q false false false false true true true false true true true true True table of logical “ or
Image of page 5
Boolean Algebra Boolean Algebra Truth tables Lists all combinations of operand values and the result of the operation for each combination P not P false true true false True table of logical “ not
Image of page 6
Boolean Algebra Boolean Algebra Can create complex logical expressions by combining simple logical expressions Example not (P and Q) A truth table can be used to determine when a logical expression is true P Q P and Q not (P and Q) false false false true true false true true false true false false true true true false
Image of page 7
A Boolean Type A Boolean Type C++ contains a type named bool Type bool has two symbolic constants true false Boolean operators The and operator is && The or operator is || The not operator is ! Warning & and | are also operators so be careful what you type
Image of page 8
A Boolean Type A Boolean Type Example logical expressions bool P = true; bool Q = false; bool R = true; bool S = (P && Q); bool T = ((!Q) || R); bool U = !(R && (!Q));
Image of page 9
Relational Operators Relational Operators Equality operators == != Examples int i = 32; int k = 45; bool q = (i == k); bool r = (i != k);
Image of page 10
Relational Operators Relational Operators Ordering operators < > >= <= Examples int i = 5; int k = 12; bool p = (i < 10); bool q = (k > i); bool r = (i >= k); bool s = (k <= 12);
Image of page 11
Operator Precedence Revisited Operator Precedence Revisited Precedence of operators (from highest to lowest) Parentheses (), [] Unary operators !, +, -, ++, --, *, &, Multiplicative operators *, /, % Additive operators +, - Relational ordering >, >=, <, <= Relational equality ==, != Logical and && Logical or || Assignment =, +=, -=, *=, /=
Image of page 12
Operator Precedence Revisited Operator Precedence Revisited Consider 5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24 Yuck! Do not write expressions like this!
Image of page 13
Operator Precedence Revisited Operator Precedence Revisited Consider 5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24 However, for your information it is equivalent to 5 *15 + 4 == 13 && 12 < 19 || !false == 5 < 24 ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( )
Image of page 14
Operator Precedence Revisited Operator Precedence Revisited Consider 5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24 However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) || ((!false) == (5 < 24)) Short -
Image of page 15
Image of page 16

You've reached the end of your free preview.

Want to read all 64 pages?

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes