C++ for Engineers and Scientists - Selection Structures

Objectives

Selection Criteria

The if-then-else Statement

Nested if Statements

The switch Statement

Applications

 

Objectives (continued)

Common Programming Errors

A Closer Look at Program Testing

 

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;

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

 

Selection Criteria (continued)

Relational expression: compares two operands or expressions using relational operators

 

 

Selection Criteria (continued)

Selection Criteria (continued)

Selection Criteria (continued)

Selection Criteria (continued)

Selection Criteria (continued)

The if-else Statement

The if-else Statement (continued)

The if-else Statement (continued)

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 (==)

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

Nested if Statements (continued)

Nested if Statements (continued)

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

 

The switch Statement (continued)

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

default case is optional

 

 

The switch Statement (continued)

 

 

The switch Statement (continued)


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

 

Objectives (continued)

Common Programming Errors

A Closer Look at Program Testing

 

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;

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

 

Selection Criteria (continued)

Relational expression: compares two operands or expressions using relational operators

 

 

Selection Criteria (continued)

Selection Criteria (continued)

Selection Criteria (continued)

Selection Criteria (continued)

Selection Criteria (continued)

The if-else Statement

The if-else Statement (continued)

The if-else Statement (continued)

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 (==)

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

Nested if Statements (continued)

Nested if Statements (continued)

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

 

The switch Statement (continued)

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

default case is optional

 

 

The switch Statement (continued)

 

 

The switch Statement (continued)