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
shortcircuit evaluation
ifelse
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?
 Winter '03
 Pro.Lei
 Logic, C++ Programming, Logical conjunction, Logical connective, endl