0% found this document useful (0 votes)
72 views

C++ Engineers and Scientists

This chapter discusses selection structures in C++, including if-else statements, nested if statements, and switch statements. If-else statements allow for two-way selection based on a conditional expression being true or false. Nested if statements allow if-else blocks to be contained within other if-else blocks. Switch statements provide multi-way selection from several alternatives based on a match between the switch expression and case values. Common errors with selection structures are discussed, such as using assignment instead of relation operators.

Uploaded by

Torres Jerome
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

C++ Engineers and Scientists

This chapter discusses selection structures in C++, including if-else statements, nested if statements, and switch statements. If-else statements allow for two-way selection based on a conditional expression being true or false. Nested if statements allow if-else blocks to be contained within other if-else blocks. Switch statements provide multi-way selection from several alternatives based on a match between the switch expression and case values. Common errors with selection structures are discussed, such as using assignment instead of relation operators.

Uploaded by

Torres Jerome
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 31

C++ for

Engineers and Scientists


Second Edition

Chapter 4
Selection Structures

Objectives

Selection Criteria
The if-then-else Statement
Nested if Statements
The switch Statement
Applications

C++ for Engineers and Scientists, Second Edit

Objectives (continued)
Common Programming Errors
A Closer Look at Program Testing

C++ for Engineers and Scientists, Second Edit

Selection Criteria
if-else statement: implements a decision
structure for two alternatives
Syntax:
if (condition)
statement executed if condition is true;
else
statement executed if condition is false;

C++ for Engineers and Scientists, Second Edit

Selection Criteria (continued)


The condition is evaluated to its numerical
value:
A non-zero value is considered to be true
A zero value is considered to be false

The else portion is optional; it is executed only


if the condition is false
The condition may be any valid C++ expression

C++ for Engineers and Scientists, Second Edit

Selection Criteria (continued)


Relational expression: compares two
operands or expressions using relational
operators

C++ for Engineers and Scientists, Second Edit

Selection Criteria (continued)


Relational expressions are evaluated to a
numerical value of 1 or 0 only:
If the value is 1, the expression is true
If the value is 0, the expression is false

char values are automatically coerced to int


values for comparison purposes
Strings are compared on a character by
character basis; the string with the first lower
character is considered smaller
C++ for Engineers and Scientists, Second Edit

Selection Criteria (continued)


Examples of string comparisons:

C++ for Engineers and Scientists, Second Edit

Selection Criteria (continued)


Logical operators
AND (&&): condition is true only if both
expressions are true
OR (||): condition is true if either one or both of
the expressions is true
NOT (!): changes an expression to its opposite
state; true becomes false, false becomes true

C++ for Engineers and Scientists, Second Edit

Selection Criteria (continued)


Precedence and Associativity of Operators

C++ for Engineers and Scientists, Second Edit

10

Selection Criteria (continued)


Comparing single and double precision values
for equality (==) can lead to errors because
values are stored in binary
Instead, test that the absolute value of the
difference is within an acceptable range
Example:

abs(operandOne operandTwo)<0.000001

C++ for Engineers and Scientists, Second Edit

11

The if-else Statement

if-else performs instructions based on the


result of a comparison
Place statements on separate lines for readability
Syntax:

C++ for Engineers and Scientists, Second Edit

12

The if-else Statement (continued)


Figure 4.2
The if-else
flowchart.

C++ for Engineers and Scientists, Second Edit

13

The if-else Statement (continued)

C++ for Engineers and Scientists, Second Edit

14

The if-else Statement (continued)


Compound statement: a sequence of single
statements contained between braces, creating a
block of statements
Block of statements can be used anywhere that a
single statement is legal
Any variable declared within a block is usable only
within that block
Scope: the area within a program where a variable
can be used; a variables scope is based on where
the variable is declared
C++ for Engineers and Scientists, Second Edit

15

The if-else Statement (continued)

C++ for Engineers and Scientists, Second Edit

16

The if-else Statement (continued)


One-way selection: an
if statement without the
optional else portion

C++ for Engineers and Scientists, Second Edit

17

The if-else Statement (continued)


Common problems with if-else statements:
Misunderstanding what an expression is
Using the assignment operator (=) instead of the
relational operator (==)

C++ for Engineers and Scientists, Second Edit

18

Nested if Statements
if-else statement can contain any valid C++
statement, including another if-else
Nested if statement: an if-else statement
completely contained within another if-else
Use braces to block code, especially when
inner if statement does not have its own
else

C++ for Engineers and Scientists, Second Edit

19

Nested if Statements (continued)


Figure 4.5a
if-else
statement
nested in an
if.

C++ for Engineers and Scientists, Second Edit

20

Nested if Statements (continued)


if-else chain: a nested if statement
occurring in the else clause of the outer ifelse
If any condition is true, the corresponding
statement is executed and the chain terminates
Final else is only executed if no conditions were
true; serves as a catch-all case
if-else chain provides one selection from
many possible alternatives
C++ for Engineers and Scientists, Second Edit

21

Nested if Statements (continued)

General form of an
Figure 4.5b if-else
if-else chain.
statement nested in an
else.
C++ for Engineers and Scientists, Second Edit
22

Nested if Statements (continued)

C++ for Engineers and Scientists, Second Edit

23

The switch Statement


switch statement: provides for one selection
from many alternatives
switch keyword starts the statement; is
followed by the expression to be evaluated
case keyword identifies a value to be compared
to the switch expression; when a match is
found, statements in this case block are
executed
All further cases after a match is found are
executed unless a break statement is found
C++ for Engineers and Scientists, Second Edit

24

The switch Statement (continued)


default case is executed if no other case
value matches were found
default case is optional

C++ for Engineers and Scientists, Second Edit

25

The switch Statement (continued)

C++ for Engineers and Scientists, Second Edit

26

The switch Statement (continued)

C++ for Engineers and Scientists, Second Edit

27

Common Programming Errors


Using the assignment operator (=) instead of
the relational operator (==) for an equality test
Assuming a structural problem with an ifelse causes the error instead of focusing on
the data value being tested
Using nested if statements without braces to
define the structure

C++ for Engineers and Scientists, Second Edit

28

Summary
Relational expressions, or conditions, are used
to compare operands
If the relation expression is true, its value is 1;
if false, its value is 0
Use logical operators && (AND), || (OR),
and ! (NOT) to construct complex conditions
if-else allows selection between two
alternatives

C++ for Engineers and Scientists, Second Edit

29

Summary (continued)
An if expression that evaluates to 0 is false; if
non-zero, it is true
if statements can be nested
Chained if statement provides a multiway
selection
Compound statement contains any number of
individual statements enclosed in braces

C++ for Engineers and Scientists, Second Edit

30

Summary (continued)
switch statement provides a multiway
selection
switch expression is evaluated and compared
to each case value; if a match is found,
execution begins at that cases statements and
continues unless a break is encountered

C++ for Engineers and Scientists, Second Edit

31

You might also like