Decision I
(if Statement)
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 that may
arise in the logical
flow of the
algorithm.
CSCE 106
3
if Statement
if (condition)
statementT;
else
optional
statementF;
two alternatives
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
CSCE 106
4
Table 4.1
CSCE 106
Rational and Equality Operators
5
Relational & Equality Operators
(cont’d)
Relational expressions (conditions) are
used to perform tests for selecting among
alternative statements to execute.
Typical forms:
variable
variable
variable
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
x >= y
false
7
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
8
Table 4.3
CSCE 106
&& Operator
9
Table 4.4
CSCE 106
|| Operator
10
Table 4.5
CSCE 106
! Operator
11
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
Output: taxDue
Additional program variables.
Processing formulas.
Design phase.
Implementation phase.
Testing phase.
CSCE 106
13
START
Design
INPUT
income
taxDue = income * 0.1
True
income <
20000 ?
False
taxDue = income * 0.2
if (income < 20000)
taxDue = income *
0.1;
else
taxDue = income *
0.2;
OUTPUT
taxDue
CSCE 106
STOP
14
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
taxDue = income * 0.1
True
if (income < 20000)
taxDue = income *
0.1;
else
income <
20000 ?
False
taxDue = income * 0.2
True
income <
30000 ?
if (income < 30000)
False
taxDue=income*0.2;
else
taxDue = income * 0.3
taxDue=income*0.3;
Nested
if Statements
CSCE 106
16
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 (condition1)
statement1;
else if (condition2)
statement2;
.
.
.
else if (conditionn)
statementn;
else
statemente;
CSCE 106
18
Example
if (x > 0)
numPos = numPos + 1;
else
if (x < 0)
numNeg = numNeg + 1;
else // x must equal 0
numZero = numZero + 1;
CSCE 106
19
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 (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
21
Next lecture we will continue
decision construct in C++
CSCE 106
22
© Copyright 2025 Paperzz