20061011 chap4 Chapter 4 Selection Structures: if and switch Statements.

Post on 18-Jan-2016

224 views 1 download

transcript

20061011 chap4

Chapter 4

Selection Structures:if and switch Statements

20061011 chap4 2

Objectives Control Structure Conditions The if Statement The switch Statement

20061011 chap4 3

Control Structures Control Structures control the flow of

execution Three kinds of execution flow:

Sequence The execution of the program is sequential

Selection A control structure which chooses alternative to

execute Repetition

A control structure which repeats a group of statements.

We will focus on the selection control structure this week.

20061011 chap4 4

Conditions A program may choose among alternative

statements by testing the value of key variables.

if (your_grade > 60) printf(“you are passed!”)

Condition is an expression that is either false (represented by 0) or true (represented by 1).

your_grade > 60

Conditions may contain relational or equality operators.

20061011 chap4 5

Relational and Equality Operators

20061011 chap4 6

Sample Conditions

20061011 chap4 7

Logical Operators To form more complicated conditions

by the three logical operators&& (and)|| (or)! (not)

Logical Expressions is an expression which uses one or more logical operators, e.g.,

salary < MIN_SALARY || dependents > 5

temperature > 90.0 && humidity > 0.9

!(0 <= n && n<= 100)

20061011 chap4 8

&& (and), || (or), ! (not)

20061011 chap4 9

Operator Precedence An operator’s precedence

determines its order of evaluation.

- x – y * z

x + y < min + max

20061011 chap4 10

Evaluation Tree & Step-by-Step Evaluation

20061011 chap4 11

Short Circuit Evaluation Stopping evaluation of a

logical expression as soon as its value can be determined.

e.g. a && b must be false if a is 0.

20061011 chap4 12

Writing English Conditions in C

20061011 chap4 13

Comparing Characters

20061011 chap4 14

Complementing a condition Complement a logical

expression with the symbol ! just change its operator

item == SENT

!(item == SENT)

item != SENT

!(a <= b) a > b

20061011 chap4 15

DeMorgan’s Theorem A way to simplify the logical expression If comp_1 is the complement of expr_1

The complement of expr_1 && expr_2 is comp_1 || comp_2

The complement of expr_1 || expr_2 is comp_1 && comp_2.

e.g. the complement of age <= 25 ||(status != ‘S’&& status!= ‘D’) is

!(age <= 25 ||(status != ‘S’&& status!= ‘D’))

age > 25 && (status == ‘S’|| status == ‘D’)

20061011 chap4 16

The if statement Syntax

if (condition) statement ;else statement ;

Ex-1.if (rest_heart_rate > 56 )

printf(“keep up your exercise program!\n”);

elseprintf(“Your heart rate is in excellent health\n”);

Ex-2.if (x != 0.0)

product = product * x;

20061011 chap4 17

Flowchart A diagram that shows the step-by-step

execution of a control structure A diamond-shape box represents a decision A rectangular box represents an

assignment statement or a process.

20061011 chap4 18

If statement with compound statements

if (condition){

true task}else{

false task}

20061011 chap4 19

Decision Steps in Algorithms Select from a choice of actions. Case study: compute a customer’s

water bill. A $35 water demand charge A consumption charge of $1.10 for

every thousand gallons used. If the customer’s unpaid balance is

greater than zero, a $2 late charge is assessed as well.

20061011 chap4 20

Structure Chart of Water Bill Problem

20061011 chap4 21

Program for Water Bill Problem

20061011 chap4 22

20061011 chap4 23

20061011 chap4 24

20061011 chap4 25

Modifying a Program with Function Subprograms Case Study: Water Bill with

Conservation Requirements Use no more than 95 percent

of the amount of water they used in the same quarter last year.

Those who do not should be charged at twice this rate.

20061011 chap4 26

Function comp_use_charge Revised

20061011 chap4 27

Nested if Statements An if statement with another if

statement as its true task or its false task

Dangling Else if (x > 0) if (y > 0)

a = a + 1;else

b = b + 1;

20061011 chap4 28

Nested if Example: Road Sign Decision Process

20061011 chap4 29

Road Sign Decision Process

20061011 chap4 30

Multiple-Alternative Decision

20061011 chap4 31

Multiple-Alternative Decision Example

20061011 chap4 32

Order of Conditions in a Multiple-Alternative Decision

When more than one condition in a multiple-alternative decision is true, only the first task following the first true condition executes.

Textbook examples

20061011 chap4 33

The switch statementWhen the

selection is based on the value of a single variable or of a simple expression (called the controlling expression).

20061011 chap4 34

Example of a switch Statement

20061011 chap4 35

Homework #4 due: 2006/10/18

讓使用者自行定義兩條直線,即輸入 6 個整數 a,b,c,d,e,f 進行例外判斷是否此二線的情況 (intersected/parallel/the

same) 讓使用者輸入一個點 S 的 2 個 整數坐標 i,j 實作出一個 function prototype

char analysis(int p, int q, int r, int x, int y);inpur argument: 某直線的 3 個係數 , 及某點的 x,y

坐標 return value: 回傳 '+', '-', '0‘ 方位利用上述 function 判斷此點 S 在某直線的方位

最後得到此點 S 在那一個區塊 ?

20061011 chap4 36

Summary

Control Structure Conditions The if Statements The switch Statements