Computer Programming
Chapter 3
Selection
Objectives
• Relational Expressions
• The if and if-else Statements
• The if-else Chain
• The switch Statement
• Case Study: Data Validation
• Common Programming and Compiler Errors
2
Introduction
• Flow of control refers to the order in which a
program’s statements are executed
3
Relational Expressions
• Simplest decision structure:
if (condition)
statement executed if condition is true
– The condition is evaluated to determine its numerical
value, which is interpreted as either true (non-zero)
or false (0)
– If condition is “true” the statement following the if is
executed; otherwise, statement is not executed
– Most commonly, a relational expression (can yield
only 0 or 1)
4
Relational Expressions (continued)
5
Relational Expressions (continued)
• Relational expressions are also known as
conditions
• A relational expression evaluates to 1 (true) or 0
(false)
– The expression 3 < 4 has a value of 1
– The expression 2.0 > 3.3 has a value of 0
– The value of hours > 0 depends on the value of
hours
6
Relational Expressions (continued)
Character data can also be compared using
relational operators
7
Logical Operators
• More complex conditions can be created using the
logical operations AND (&&), OR (||), and NOT (!)
• When the && is used with two expressions, the
condition is true only if both expressions are true by
themselves
8
Logical Operators
9
Logical Operators (continued)
int i = 15, j = 30;
double a = 12.0, b = 2.0, complete = 0.0;
10
Logical Operators (continued)
11
Logical Operators (continued)
char key = 'm';
int i = 5, j = 7, k = 12;
double x = 22.5;
12
The if and if-else Statements
O
n
e
-
w
a
y
i
No semicolon here
f
s
t
a
t
e
13
m
The if and if-else Statements
(continued)
14
Compound Statements
• Although only a single statement is permitted in an
if statement, this statement can be a single
compound statement
15
Compound Statements (continued)
• For example,
if (expression)
{
statement1; /*as many statements as necessary*/
statement2; /*can be placed within the braces*/
• /*each statement must end with ; */
•
•
statementn;
}
• For very short statements, you can code a complete
if statement placed on a single line
– if (grade > 69) ++passTotal;
16
The if-else Statement
• The most commonly
used if-else
statement is
if (expression)
statement1;
else
statement2;
– If the value of
expression is 0
statement2, the
statement after the
reserved word else,
is executed
17
The if-else Statement (continued)
18
The if-else Statement (continued)
19
The if-else Chain
• Nested if statement:
if (expression1)
statement1;
else
if (expression2)
statement2;
else
statement3;
• Whether the indentation exists or not, the compiler
will, by default, associate an else with the closest
previous unpaired if, unless braces are used to
alter this default pairing
20
The if-else Chain (continued)
• if-else chain:
if (expression1)
statement1;
else if (expression2)
statement2;
else
statement3;
21
The if-else Chain (continued)
22
The if-else Chain (continued)
23
The if-else Chain (continued)
24
The switch Statement
Terminated with a colon
If the break statement was omitted,
the following case would be executed
default is optional
25
The switch Statement (continued)
26
The switch Statement (continued)
27
Case Study: Data Validation
• Defensive programming is a technique where the
program includes code to check for improper data
before an attempt is made to process it further
– Checking user input data for erroneous or
unreasonable data is called input data validation
• Requirements:
– Write a program to calculate the square root and the
reciprocal of a user-entered number. Validate that
the number is not negative before attempting to take
its square root, and that the number is not 0 before
calculating the number’s reciprocal value.
28
Case Study: Data Validation
(continued)
29
Common Programming Errors
• Using the assignment operator, =, in place of the
relational operator, ==
• Letting the if-else statement appear to select an
incorrect choice
• Nesting if statements without including braces to
clearly indicate the desired structure
• Using a single & or | in place of the logical && and
logical || operators, respectively
30
Common Compiler Errors
31
Summary
• Relational expressions, which are also called simple
conditions, are used to compare operands
• Conditions can be constructed from relational
expressions using C’s logical operators, &&, ||, and !
• A one-way if statement has the general form
if (expression)
statement;
• A compound statement consists of any number of
individual statements enclosed within braces
• An if-else selects between two alternative
statements based on the value of an expression
32
Summary (continued)
• An if-else statement can contain other if-else
statements
• The if-else chain is a multiway selection
statement
• The switch statement is a multiway selection
statement; program execution is transferred to the
first matching case and continues through the end
of the switch statement unless an optional break
statement is encountered
33