ch04 selection

28
Chapter 4 Selection 

Upload: fazumia

Post on 06-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 1/28

Chapter 4Selection 

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 2/28

Selection 

A First Book of ANSI C, Fourth Edition 2

Introduction

Flow of control refers to the order in which aprogram’s statements are executed 

Any algorithm can be built using combinations of fourstandardized flow of control structures:

Normal flow of control for all programs issequential 

Selection  is used to select which statements areperformed next based on a condition

Repetition  is used to repeat a set of statements Invocation is used to invoke a sequence of

instructions using a single statement, as in callinga function

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 3/28

Selection 

A First Book of ANSI C, Fourth Edition 3

Relational Expressions

Simplest decision structure:

if (condition) 

statement executed if condition is true;  

The condition is evaluated to determine its numericalvalue, which is interpreted as either true (non-zero) orfalse (0)

If condition is “true” the statement following the if isexecuted; otherwise, statement is not executed

The condition used in all of C’s if statements can beany valid C expression

Most commonly, a relational expression (can yieldonly 0 or 1)

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 4/28

Selection 

A First Book of ANSI C, Fourth Edition 4

Relational Expressions(continued)

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 5/28

Selection 

A First Book of ANSI C, Fourth Edition 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 ofhours

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 6/28

Selection 

A First Book of ANSI C, Fourth Edition 6

Relational Expressions(continued)

Character data can also be compared using relational operators

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 7/28

Selection 

A First Book of ANSI C, Fourth Edition 7

More complex conditions can be created using thelogical operations AND (&&), OR (||), and NOT (!)

When the && is used with two expressions, thecondition is true only if both expressions are true by

themselves

int counter = 25;counter >10 && counter <=25 TRUEcounter >10 && counter <20 FALSE

Example:

Logical Operators

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 8/28

Selection 

A First Book of ANSI C, Fourth Edition 8

Logical Operators(continued)

When the || is used with two expressions, the condition istrue if any one of the expressions is true

int counter = 25;counter >10 || counter <=25 TRUEcounter >30 || counter <=25 TRUEcounter >30 || counter <25 FALSE

Example:

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 9/28

Selection 

A First Book of ANSI C, Fourth Edition 9

Logical Operators(continued)

When the ! is used with an expression, it will return theopposite condition.

! (counter == 25) is equal to counter !=25

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 10/28

Selection 

A First Book of ANSI C, Fourth Edition 10

Logical Operators(continued)

int i = 15, j = 30;

double a = 12.0, b = 2.0, complete = 0.0;

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 11/28

Selection 

A First Book of ANSI C, Fourth Edition 11

Logical Operators(continued)

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 12/28

Selection 

A First Book of ANSI C, Fourth Edition 12

Logical Operators(continued)

char key = 'm';

int i = 5, j = 7, k = 12;

double x = 22.5;

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 13/28

Selection 

A First Book of ANSI C, Fourth Edition 13

The if and if-else 

Statements

No semicolon hereOne-way if statement

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 14/28

Selection 

A First Book of ANSI C, Fourth Edition 14

The if and if-else 

Statements (continued)

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 15/28

Selection 

A First Book of ANSI C, Fourth Edition 15

Compound Statements

Although only a single statement is permittedin an if statement, this statement can be a

single compound statement 

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 16/28

Selection 

A First Book of ANSI C, Fourth Edition 16

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;

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 17/28

Selection 

A First Book of ANSI C, Fourth Edition 17

The if-else Statement

The most commonly used if-else statement

isif (expression) 

statement1;else 

statement2;

If the value of expression is 0 (false):

•statement1 is skipped,

•statement2 (the statement after the reservedword else), is executed

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 18/28

Selection 

A First Book of ANSI C, Fourth Edition 18

The if-else Statement

(continued)

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 19/28

Selection 

A First Book of ANSI C, Fourth Edition 19

The if-else Statement

(continued)

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 20/28

Selection 

A First Book of ANSI C, Fourth Edition 20

The if-else Statement

(continued)

Compoundedstatements

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 21/28

Selection 

A First Book of ANSI C, Fourth Edition 21

The if-else Chain

Nested if statement: if (expression1)

statement1;

else

if (expression2 )statement2;

else

statement3; 

Whether the indentation exists or not, thecompiler will, by default, associate an else withthe closest previous unpaired if, unless braces

are used to alter this default pairing 

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 22/28

Selection 

A First Book of ANSI C, Fourth Edition 22

The if-else Chain

(continued)

if-else chain:if (expression1)

statement1;

else if (expression2 )

statement2;

else

statement3; 

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 23/28

Selection 

A First Book of ANSI C, Fourth Edition 23

The if-else Chain

(continued)

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 24/28

Selection 

A First Book of ANSI C, Fourth Edition 24

The if-else Chain

(continued)

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 25/28

Selection 

A First Book of ANSI C, Fourth Edition 25

The if-else Chain

(continued)

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 26/28

Selection 

A First Book of ANSI C, Fourth Edition 26

The switch Statement

Terminated with a colon

default is optional

If the break statement was omitted,the following case would be executed

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 27/28

Selection 

A First Book of ANSI C, Fourth Edition 27

The switch Statement

(continued)

8/3/2019 Ch04 Selection

http://slidepdf.com/reader/full/ch04-selection 28/28

Selection 

A Fi B k f ANSI C F h Edi i 28

The switch Statement

(continued)