Decision I if Statement CSCE 106 Outline What

  • Slides: 22
Download presentation
Decision I (if Statement) CSCE 106

Decision I (if Statement) CSCE 106

Outline § What is Decision § if statement (section 4. 3) § Relational &

Outline § What is Decision § if statement (section 4. 3) § Relational & equality operators § Logical operators § Multiple Decision - Nested if statements (section 4. 7) § if statement with compound alternatives (section 4. 4) CSCE 106 2

Decision Or selection is the choice of alternate paths (branches) depending on a condition

Decision Or selection is the choice of alternate paths (branches) depending on a condition that may arise in the logical flow of the algorithm. CSCE 106 3

if Statement if (condition) statement. T; else optional statement. F; two alternatives § if

if Statement if (condition) statement. T; else optional statement. F; two alternatives § if allows a question to be asked § There are two basic forms for an if statement: a dependent (conditional) statement u a choice between two alternatives u CSCE 106 4

Table 4. 1 CSCE 106 Rational and Equality Operators 5

Table 4. 1 CSCE 106 Rational and Equality Operators 5

Relational & Equality Operators (cont’d) § Relational expressions (conditions) are used to perform tests

Relational & Equality Operators (cont’d) § Relational expressions (conditions) are used to perform tests for selecting among alternative statements to execute. § Typical forms: variable relational-operator variable relational-operator constant equality-operator variable equality-operator constant § Evaluate to Boolean (bool) value of true or false CSCE 106 6

Examples x y -5 -5 x <= 0 true CSCE 106 -5 7 7

Examples x y -5 -5 x <= 0 true CSCE 106 -5 7 7 x >= y false 7

Logical Operators § What if you have more complex conditions, like checking ranges (0<x<y)?

Logical Operators § What if you have more complex conditions, like checking ranges (0<x<y)? § The normal way you represent ranges is not valid in programming. § You use logical operators to form more complex conditions && (and) || (or) ! (not) § E. g. (10000 < salary) && (salary <= 20000) (salary < min. Salary) || (dependents > 5) (temperature > 90. 0) && (humidity > 0. 90) winning. Record && (!probation) CSCE 106 8

Table 4. 3 CSCE 106 && Operator 9

Table 4. 3 CSCE 106 && Operator 9

Table 4. 4 CSCE 106 || Operator 10

Table 4. 4 CSCE 106 || Operator 10

Table 4. 5 CSCE 106 ! Operator 11

Table 4. 5 CSCE 106 ! Operator 11

Exercise Please analyse, design and implement an algorithm to calculate and output the tax

Exercise Please analyse, design and implement an algorithm to calculate and output the tax due according to the annual income which is input by the user. The tax rate should be calculated as follows: Tax rate = 10% for annual income < 20, 000 LE = 20% for annual income >= 20, 000 CSCE 106 12

Solution Steps § Problem statement/Requirements phase. § Analysis Input: income u Output: tax. Due

Solution Steps § Problem statement/Requirements phase. § Analysis Input: income u Output: tax. Due u Additional program variables. u Processing formulas. u § Design phase. § Implementation phase. § Testing phase. CSCE 106 13

START § Design INPUT income tax. Due = income * 0. 1 True income

START § Design INPUT income tax. Due = income * 0. 1 True income < 20000 ? False tax. Due = income * 0. 2 if (income < 20000) tax. Due = income * 0. 1; else tax. Due = income * 0. 2; OUTPUT tax. Due CSCE 106 STOP 14

Multiple Selection § Problem Modification Tax rate = 10% for annual income < 20,

Multiple Selection § Problem Modification Tax rate = 10% for annual income < 20, 000 LE = 20% for 20, 000<=annualincome<30000 = 30% for annual income >= 30, 000 CSCE 106 15

§ Design Modification tax. Due = income * 0. 1 True if (income <

§ Design Modification tax. Due = income * 0. 1 True if (income < 20000) tax. Due = income * 0. 1; else income < 20000 ? False tax. Due = income * 0. 2 True income < 30000 ? if (income < 30000) tax. Due=income*0. False 2; tax. Due = income * 0. 3 else tax. Due=income* 0. 3; Nested if Statements CSCE 106 16

Nested if Statements § Nested logic, in general, is one control structure containing another

Nested if Statements § Nested logic, in general, is one control structure containing another similar control structure § E. g. one if statement inside another § Makes it possible to code decisions with several alternatives CSCE 106 17

General Form if (condition 1) statement 1; else if (condition 2) statement 2; .

General Form if (condition 1) statement 1; else if (condition 2) statement 2; . . . else if (conditionn) statementn; else statemente; CSCE 106 18

Example if (x > 0) num. Pos = num. Pos + 1; else if

Example if (x > 0) num. Pos = num. Pos + 1; else if (x < 0) num. Neg = num. Neg + 1; else // x must equal 0 num. Zero = num. Zero + 1; CSCE 106 19

if Statements with Compound Alternatives § Uses { } to group multiple statements. §

if Statements with Compound Alternatives § Uses { } to group multiple statements. § Any statement type (e. g. assignment, function call, if) can be placed within { }. § Entire group of statements within { } are either all executed or all skipped when part of an if statement. CSCE 106 20

Example if (transaction. Type == ‘c’) { // process check cout << “Check for

Example if (transaction. Type == ‘c’) { // process check cout << “Check for $” << transaction. Amount << endl; balance = balance - transaction. Amount; } else { // process deposit cout << “Deposit of $” << transaction. Amount << endl; balance = balance + transaction. Amount; } CSCE 106 21

Next lecture we will continue decision construct in C++ CSCE 106 22

Next lecture we will continue decision construct in C++ CSCE 106 22