Control Structures
Relational Operators
cont’d
Here there are some examples:
1 (2.5 == 5.8) // evaluates to false
2 (5 > 4) // evaluates to true
3 (3 != 2) // evaluates to true
4 (6 >= 6) // evaluates to true
5 ('R’ > 'T’) // evaluates to false
Of course, it's not just numeric constants that can be compared,but just any value,
including, of course, variables. Suppose that a=2, b=3 and c=6, then:
1 (a == 5) // evaluates to false, since a is not equal to 5
2 (a*b >= c) // evaluates to true, since (2*3 >= 6) is true
3 (b+4 > a*c) // evaluates to false, since (3+4 > 2*6) is false
4 ((b=2) == a) // evaluates to true
Logical (Boolean) Operators and Logical
Expressions
• Logical (Boolean) operators: let you combine
logical expressions.
The ! (Not) Operator
The && (And) Operator
The || (Or) Operator
Precedence of Some Operators
Precedence of Operators (cont’d.)
Precedence of Operators (cont’d.)
Precedence of Operators (cont’d.)
Short-Circuit Evaluation
• Short-circuit evaluation: evaluation of a logical
expression stops as soon as the value of the
expression is known.
• Example:
(age >= 21) || ( x == 5) //Line 1
(grade == 'A') && (x >= 7) //Line 2
Three Common Mistakes
• The next three slides discuss three common
mistakes:
1. Comparing floating-point numbers for equality.
2. Associativity of relational operators.
3. Confusing equality (==) with assignment (=).
Comparing Floating-Point Numbers for
Equality: A Precaution
• Comparison of floating-point numbers for equality
may not behave as you would expect.
– Example:
• 1.0 == 3.0/7.0 + 2.0/7.0 + 2.0/7.0 evaluates to
false
• Why? 3.0/7.0 + 2.0/7.0 + 2.0/7.0 =
0.99999999999999989
• Solution: either avoid doing this, or use a tolerance
value instead:
– Example: if fabs(x – y) < 0.000001
Associativity of Relational Operators: A
Precaution
Associativity of Relational Operators: A
Precaution (cont’d.)
• Suppose num = 5:
• Next, suppose num = 20:
One-Way Selection
One-Way Selection (cont’d.)
• One-way selection syntax:
• statement is executed if the value of the
expression is true.
• But statement is bypassed if the value is false;
program goes to the next statement.
• expression is called a decision maker.
One-Way Selection: Example
Two-Way Selection
Two-Way Selection (cont’d.)
• Two-way selection syntax:
• If expression is true, statement1 is
executed; otherwise, statement2 is executed.
– statement1 and statement2 are any C++
statements.
Two-Way Selection: Example
Compound Statements
• Compound statement (block of statements):
• A compound statement functions like a single statement.
Compound Statements: Example
Compound Statements (cont’d.)
if (age > 18)
{
cout << "Eligible to vote." << endl;
cout << "No longer a minor." << endl;
}
else
{
cout << "Not eligible to vote." << endl;
cout << "Still a minor." << endl;
}
Multiple Selections: Nested if
• Nesting: one control statement is located within
another.
• An else is associated with the most recent if
that has not been paired with an else.
Multiple Selections: Nested if (cont’d.)
Comparing if…else Statements with a
Series of if Statements
• The following slide shows another (less efficient)
way to accomplish the same task.
Comparing if…else Statements with a
Series of if Statements (cont’d.)
• This code accomplishes the same task as the code
on the previous slide, but it’s not as efficient.
Alternatives to if and if…else
• Using if statements and if…else statements, you
can handle any decision-making situations that your
programs may require.
• But C++ does offer two other ways to do the same sort
of thing. You may wish to use these instead of if and
if…else:
• The conditional operator ?:
• The switch structure
Conditional Operator ?:
• The conditional operator looks like this: ?:
– It’s a ternary operator: takes 3 arguments.
• Syntax:
expression1 ? expression2 : expression3
• If expression1 is true, the result of the entire
expression is expression2.
– Otherwise, the result is expression3.
• Concise and efficient, but hard to read.
• See next slide for example.
Conditional Operator ?: (cont.)
• Example: max = (a >= b) ? a : b;
• The preceding statement is equivalent to the
following code:
if (a >= b)
max = a;
else
max = b;
switch Structures
• Syntax is shown at right.
• (expression) is evaluated first,
and it must evaluate to an integer.
• Value of expression determines
which corresponding action is taken.
• Expression is sometimes
called the selector.
switch Structures (cont’d.)
36
switch Structures (cont’d.)
• One or more statements may follow a case label.
• Braces are not needed to turn multiple statements into
a single compound statement.
• When a case value is matched, all statements after it
execute until a break is encountered.
• The break statement may or may not appear after
each statement.
• switch, case, break, and default are reserved
words.
switch Structures (cont’d.)
Avoiding Bugs: Revisited
• To output results correctly:
– Consider whether the switch structure must
include a break statement after each cout
statement.