Chapter 3 Part A
Chapter 3 Part A
Control Structures
A computer can proceed:
in sequence
selectively (branch) - making a choice
repetitively (iteratively) - performing a
statement over and over, also called a loop
Some statements are executed only if certain
conditions are met
A condition is represented by a logical
(Boolean) expression that has a value of
either true or false
A condition is met if it evaluates to true
Relational Operators
A relational operator allows you to make
comparisons in a program
Equality of real numbers is usually machine
dependent so it is possible that on a
particular machine 6.8 + 3.1 == 2.7 + 7.2
might evaluate to false
Comparing values of different data types may
produce unpredictable results
For example, 8 < '5' should not be done
C++ returns an integer value of 1 if the
logical expression evaluates to true and the
value of 0 otherwise
In C++, any nonzero value is treated as true
Relational Operator
The following expressions are
true.
c == e
i != k
i<j
d>e
i <= k
j >= k
Example 2:
Given the following declarations:
int i = 1;
int j = 2;
int k = 2;
char c = 2;
char d = 3;
And the following expressions are
the false.
char e = 2;
i == j
c != e
j<k
c>e
d <= c
i >= k
!p
true
false
false
true
p2
p1 && p2
false
false
false
false
true
false
true
false
false
true
true
true
p2
p1 || p2
false
false
false
false
true
true
true
false
true
true
true
true
Short-Circuit Operator
When evaluating p1 && p2, C++ first evaluates p1 and then
evaluates p2 if p1 is true; if p1 is false, it does not evaluate p2.
When evaluating p1 || p2, C++ first evaluates p1 and then
evaluates p2 if p1 is false; if p1 is true, it does not evaluate p2.
Therefore, && is referred to as the conditional or short-circuit
AND operator, and || is referred to as the conditional or shortcircuit OR operator.
Examples
Write a program that lets the user enter a year and checks whether
it is a leap year.
A year is a leap year if it is divisible by 4 but not by 100 or if it is
divisible by 400. So you can use the following Boolean
expression to check whether a year is a leap year:
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
Precedence of Operators
Relational and logical operators are evaluated
from left to right so the associativity is said to be
from left to right
Parentheses can be used to indicate an order of
operation that is different than the order
indicated by order of precedence
Short-circuit evaluation (of a logical expression) a process in which the computer evaluates a
logical expression from left to right and stops as
soon as the value of the expression is known
Logical Expression
Example 1:
(gender == m) && (age > 20)
Example 2:
5 * 15 + 4 == 13 && 12 < 19 || !false
== 5 < 24
It is equivalent to:
((((5 *15) + 4) == 13) && (12 < 19))||((!
false) == (5 < 24))
Troubleshooting
First mistake:
suppose a programmer needs to check if x
is greater than
both y and z
x > y && z
operation
(x > y && x > z)
// illegal
// correct way
Troubleshooting
Second mistake:
the difference between relational operator,
= = and assignment operator, =
a = b
a == b
and b
// assign b into a
// check to see if a
// have same value
Troubleshooting
Third mistake:
suppose a programmer needs to check if A
or B is true and if C or D is true
( A || B && C || D)
((A || B) && (C || D))
way
// illegal
// correct
Value
Value