Decision I (if Statement) Control structures regulate the flow of execution. So far, you know of three types of control structure: sequential, selection, repetition if statement is used for selection/decision in C++
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
Decision Or selection is the choice of alternate paths (branches) depending on a condition that may arise in the logical flow of the algorithm. Decision/Selection construct is a question with two outcomes. In flow charts the notation used for decision is the diamond shape with the condition/question written inside. The diamond shape has one arrow going in and two arrows marked true or false coming out. Notice how the two selection diagrams differ. One with two alternatives, and one with only a dependant statement. CSCE 106
if Statement if allows a question to be asked if (condition) statementT; else statementF; if allows a question to be asked There are two basic forms for an if statement: a dependent (conditional) statement a choice between two alternatives two alternatives optional else is optional in the structure of an if statement. if with Dependent Statement When condition evaluates to true, the statement is executed; when false, it is skipped E.g.: if (x != 0) product = product * x; CSCE 106
Table 4.1 Rational and Equality Operators CSCE 106
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 variable relational-operator constant variable equality-operator variable variable equality-operator constant Evaluate to Boolean (bool) value of true or false CSCE 106
Examples x x <= 0 y x >= y -5 7 -5 -5 7 true false CSCE 106
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 < minSalary) || (dependents > 5) (temperature > 90.0) && (humidity > 0.90) winningRecord && (!probation) CSCE 106
Table 4.3 && Operator CSCE 106
Table 4.4 || Operator CSCE 106
Table 4.5 ! Operator CSCE 106
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 The selection construct has to be used to solve this exercise. CSCE 106
Solution Steps Problem statement/Requirements phase. Analysis Input: income Output: taxDue Additional program variables. Processing formulas. Design phase. Implementation phase. Testing phase. Let’s first start with analysing the problem. What is the input, output, and processing? Then design by drawing the flow chart. Please draw the flow chart in your notes. CSCE 106
Design if (income < 20000) taxDue = income * 0.1; else START Design INPUT income True income < 20000 ? if (income < 20000) taxDue = income * 0.1; else taxDue = income * 0.2; taxDue = income * 0.1 False taxDue = income * 0.2 OUTPUT taxDue STOP CSCE 106
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
Design Modification Nested if Statements if (income < 20000) taxDue = income * 0.1; else if (income < 30000) taxDue=income*0.2; taxDue=income*0.3; True income < 20000 ? taxDue = income * 0.1 False True income < 30000 ? taxDue = income * 0.2 False taxDue = income * 0.3 Nested if Statements CSCE 106
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
General Form if (condition1) statement1; else if (condition2) . else if (conditionn) statementn; else statemente; CSCE 106
Example if (x > 0) numPos = numPos + 1; else if (x < 0) numNeg = numNeg + 1; else // x must equal 0 numZero = numZero + 1; CSCE 106
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
Example if (transactionType == ‘c’) { // process check cout << “Check for $” << transactionAmount << endl; balance = balance - transactionAmount; } else { // process deposit cout << “Deposit of $” << transactionAmount << endl; balance = balance + transactionAmount; CSCE 106
Next lecture we will continue decision construct in C++ CSCE 106