Download presentation
Presentation is loading. Please wait.
1
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++
2
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
3
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
4
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
5
Table 4.1 Rational and Equality Operators
CSCE 106
6
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
7
Examples x x <= 0 y x >= y -5 7 -5 -5 7 true false CSCE 106
8
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
9
Table 4.3 && Operator CSCE 106
10
Table 4.4 || Operator CSCE 106
11
Table 4.5 ! Operator CSCE 106
12
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
13
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
14
Design if (income < 20000) taxDue = income * 0.1; else
START Design INPUT income True income < ? 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
15
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
16
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 < ? taxDue = income * 0.1 False True income < ? taxDue = income * 0.2 False taxDue = income * 0.3 Nested if Statements CSCE 106
17
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
18
General Form if (condition1) statement1; else if (condition2)
. else if (conditionn) statementn; else statemente; CSCE 106
19
Example if (x > 0) numPos = numPos + 1; else if (x < 0)
numNeg = numNeg + 1; else // x must equal 0 numZero = numZero + 1; CSCE 106
20
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
21
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
22
Next lecture we will continue decision construct in C++
CSCE 106
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.