Chapter 4
CONTROL STRUCTURES
(SELECTION)
Objectives
In this chapter, you will:
• Learn about control structures
• Examine relational and logical operators
• Explore how to form and evaluate logical
(Boolean) expressions
• Discover how to use the selection control
structures if, if...else, and switch in a
program
2
Control Structures
• A computer can proceed:
– In sequence
– Selectively (branch) - making a choice
– Repetitively (iteratively) - looping
• Some statements are executed only if
certain conditions are met
• A condition is met if it evaluates to true
3
Control Structures (cont.)
4
Selection: if and if...else
• One-Way Selection
• Two-Way Selection
– Compound (Block of) Statements
• Multiple Selections
• Nested if
– Comparing if...else Statements with a
Series of if Statements
5
Selection: if and if...else
(cont.)
• The syntax of one-way selection is:
• The statement is executed if the value of the
expression is true
• The statement is bypassed if the value is false;
program goes to the next statement
• if is a reserved word
6
One-Way Selection (cont.)
7
cout << "Enter integer: ";
cin >> number;
temp = number;
if (number < 0)
number = -number;
cout << "The absolute value of " << temp << "is " << number << endl;
cout << “Thank you”;
8
Two-Way Selection
• Two-way selection takes the form:
• If expression is true, statement1 is executed;
otherwise, statement2 is executed
– statement1 and statement2 are any C++
statements
• else is a reserved word
9
Two-Way Selection (cont.)
10
Two-Way Selection (cont.)
11
Compound (Block of) Statement
• Compound statement (block of statements):
• A compound statement is a single statement
12
Compound (Block of) Statement
(cont.)
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; }
cout << “thank you..”;
13
Multiple Selections:
• Nesting: one control statement in another
• An else is associated with the most recent if
that has not been paired with an else
14
15
Comparing if…else Statements
with a Series of if Statements
16
Confusion Between == and =
• C++ allows you to use any expression that
can be evaluated to either true or false
as an expression in the if statement:
if (x = 5)
cout << "The value is five." << endl;
• The appearance of = in place of ==
resembles a silent killer
– It is not a syntax error
– It is a logical error
17
switch Structures
• switch structure: alternate
to if-else
• switch (integral)
expression is evaluated first
• Value of the expression
determines which
corresponding action is
taken
• Expression is sometimes
called the selector
18
19
switch Structures (cont.)
• One or more statements may follow a
case label
• Braces are not needed to turn multiple
statements into a single compound
statement
• The break statement may or may not
appear after each statement
• switch, case, break, and default are
reserved words
20
21
Summary
• Control structures alter normal control flow
• Most common control structures are selection
and repetition
• Relational operators: ==, <, <=, >, >=, !=
• Logical expressions evaluate to 1 (true) or 0
(false)
• Logical operators: ! (not), && (and), || (or)
• Two selection structures: one-way selection and
two-way selection
• The expression in an if or if...else structure
is usually a logical expression
22
Summary (continued)
• No stand-alone else statement in C++
– Every else has a related if
• A sequence of statements enclosed between
braces, { and }, is called a compound statement
or block of statements
• Using assignment in place of the equality
operator creates a semantic error
• switch structure handles multiway selection
• break statement ends switch statement
Source:
C++ Programming: From Problem Analysis to Program Design,
23
Fourth Edition