Download presentation
Presentation is loading. Please wait.
Published byRandolph Reynolds Modified over 9 years ago
1
20061011 chap4 Chapter 4 Selection Structures: if and switch Statements
2
20061011 chap4 2 Objectives Control Structure Conditions The if Statement The switch Statement
3
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.
4
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.
5
20061011 chap4 5 Relational and Equality Operators
6
20061011 chap4 6 Sample Conditions
7
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 5 temperature > 90.0 && humidity > 0.9 !(0 <= n && n<= 100)
8
20061011 chap4 8 && (and), || (or), ! (not)
9
20061011 chap4 9 Operator Precedence An operator ’ s precedence determines its order of evaluation. - x – y * z x + y < min + max
10
20061011 chap4 10 Evaluation Tree & Step-by-Step Evaluation
11
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.
12
20061011 chap4 12 Writing English Conditions in C
13
20061011 chap4 13 Comparing Characters
14
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
15
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’)
16
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”); else printf(“Your heart rate is in excellent health\n”); Ex-2. if (x != 0.0) product = product * x;
17
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.
18
20061011 chap4 18 If statement with compound statements if (condition) { true task } else { false task }
19
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.
20
20061011 chap4 20 Structure Chart of Water Bill Problem
21
20061011 chap4 21 Program for Water Bill Problem
22
20061011 chap4 22
23
20061011 chap4 23
24
20061011 chap4 24
25
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.
26
20061011 chap4 26 Function comp_use_charge Revised
27
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;
28
20061011 chap4 28 Nested if Example: Road Sign Decision Process
29
20061011 chap4 29 Road Sign Decision Process
30
20061011 chap4 30 Multiple-Alternative Decision
31
20061011 chap4 31 Multiple-Alternative Decision Example
32
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
33
20061011 chap4 33 The switch statement When the selection is based on the value of a single variable or of a simple expression (called the controlling expression).
34
20061011 chap4 34 Example of a switch Statement
35
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在那一個區塊?
36
20061011 chap4 36 Summary Control Structure Conditions The if Statements The switch Statements
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.