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
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
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
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
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
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
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
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
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));
Relational Operators Relational Operators Equality operators == != Examples int i = 32; int k = 45; bool q = (i == k); bool r = (i != k);
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);
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 =, +=, -=, *=, /=
Operator Precedence Revisited Operator Precedence Revisited Consider 5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24 Yuck! Do not write expressions like this!
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 ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( )
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 -

#### You've reached the end of your free preview.

Want to read all 64 pages?

### What students are saying

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

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

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

Dana University of Pennsylvania ‘17, Course Hero Intern

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

Jill Tulane University ‘16, Course Hero Intern

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