You are on page 1of 25

Lecture 4:

Selection Statements

November 3, 2021 Prof. Abdelaziz Khamis 1


Lecture 4 Topics
 Control Structures
 Relational Operators
 Comparing string Types
 Logical (Boolean) Operators
 Precedence of Operators
 Logical (Boolean) Expressions
 One-Way (if) Selection
 Two-Way (if…else) Selection
 Compound (Block of) Statement
 Multiple Selections: Nested if
 Conditional Operator (?:)
 switch Structures
November 3, 2021 Prof. Abdelaziz Khamis 2
Control Structures
 A computer can proceed in: sequence, selection (making a
choice), or repetition (looping)

November 3, 2021 Prof. Abdelaziz Khamis 3


Control Structures (Continued)
 In selection and repetition structures, some statements
are executed only if certain conditions are met.
 A condition is represented by a logical expression that
can be true or false.
 A condition is met if it evaluates to true.
 true and false are logical (Boolean) values.
 Any nonzero value is treated as true.

November 3, 2021 Prof. Abdelaziz Khamis 4


Relational Operators
 C++ includes six relational operators that allow you to
state conditions and make comparisons.

 Each of the relational operators is a binary operator; that


is, it requires two operands.
 The result of a comparison is true or false.

November 3, 2021 Prof. Abdelaziz Khamis 5


Relational Operators (Continued)
 Relational Operators and Simple Data Types:
 You can use the relational operators with all the simple data
types: integers, real numbers, and characters.
 Comparing integers and real numbers:

November 3, 2021 Prof. Abdelaziz Khamis 6


Relational Operators (Continued)
 Relational Operators and Simple Data Types:

 Comparing characters:

November 3, 2021 Prof. Abdelaziz Khamis 7


Relational Operators (Continued)
 Relational Operators and the string Type:
 Relational operators can be applied to strings.
 Strings are compared character by character, starting with
the first character.
 Comparison continues until either a mismatch is found or all
characters are found equal.
 If two strings of different lengths are compared and the
comparison is equal to the last character of the shorter
string, the shorter string is less than the larger string.

November 3, 2021 Prof. Abdelaziz Khamis 8


Relational Operators (Continued)
 Relational Operators and the string Type:
 An example: suppose we have the following declarations:
string str1 = "Hello";
string str2 = "Hi";
string str3 = "Air";
string str4 = "Bill";

November 3, 2021 Prof. Abdelaziz Khamis 9


Logical (Boolean) Operators
 Logical (Boolean) operators enable you to combine
logical expressions.
 C++ has three logical (Boolean) operators:
 ! – not
 && – and
 || – or
 ! is unary; && and || are binary operators
 Putting ! in front of a logical expression reverses its value
 Exp1 || Exp2 is true if and only if at least one of the
operands, Exp1 or Exp2, is true
 Exp1 && Exp2 is true if and only if both the operands,
Exp1 and Exp2, are true
November 3, 2021 Prof. Abdelaziz Khamis 10
Precedence of Operators
 Relational and logical operators are evaluated from left to
right. Parentheses can override precedence

November 3, 2021 Prof. Abdelaziz Khamis 11


Logical (Boolean) Expressions
 Recent versions of C++ contain a built-in data type, bool,
that has the logical (Boolean) values true and false.
 bool, true, and false are reserved words
 The identifier true has the value 1
 The identifier false has the value 0
 An example:
Consider the declaration:
bool legalAge;
int age;
The statement:
legalAge = (age >= 21);
assigns the value true to legalAge if the value of age is
greater than or equal to 21. Otherwise, assigns the value
false to legalAge.
November 3, 2021 Prof. Abdelaziz Khamis 12
Logical (Boolean) Expressions (Continued)
 The following expression:
0 <= num <= 10
always evaluates true because 0 <= num evaluates to either
0 or 1, and 0 <= 10 is true and 1 <= 10 is true
 The correct way to write this expression is:
0 <= num && num <= 10

 Short-circuit evaluation: evaluation of a logical expression


stops as soon as the value of the expression is known.
 An Example:
(age >= 21) || ( x == 5)
(grade == ‘A’) && (x >= 7)
November 3, 2021 Prof. Abdelaziz Khamis 13
One-Way (if) Selection
 The syntax of one-way selection is:
if (expression)
statement

 if is a reserved word
 expression is usually a logical expression
 statement is any C++ statement
 statement is executed if the value of expression is true
 statement is bypassed if the value of expression is false;
program goes to the next statement

November 3, 2021 Prof. Abdelaziz Khamis 14


One-Way (if) Selection (Continued)
 Example
int main()
{
int score;
string grade = "Fail";
cin >> score;
if (score >= 60)
grade = "Pass";
cout << grade;
return 0;
}

November 3, 2021 Prof. Abdelaziz Khamis 15


Two-Way (if…else) Selection
 The syntax of two-way selection is:
if (expression)
statement1
else
statement2
 else is a reserved word
 statement1 and statement2 are any C++ statements.
 If expression is true, statement1 is executed; otherwise
statement2 is executed.

November 3, 2021 Prof. Abdelaziz Khamis 16


Two-Way (if…else) Selection (Continued)
 Example
int main()
{
int score;
string grade;
cin >> score;
if (score >= 60)
grade = "Pass";
else
grade = "Fail";
cout << grade;
return 0;
}
November 3, 2021 Prof. Abdelaziz Khamis 17
Compound (Block of) Statement
 A compound statement takes the following form:
{
statement1;
statement2;
...
statementn;
}
 A compound statement is a single statement.
 Compound statements are used to execute more than
one statement if the expression in an if or if. . .else
statement evaluates to true.
 Compound statements are used to execute more than
one statement if the expression in an if. . .else statement
evaluates to false.
November 3, 2021 Prof. Abdelaziz Khamis 18
Compound (Block of) Statement
 Example
int main()
{
int score;
string grade;
cin >> score;
if (score >= 60)
{grade = "Pass"; cout << grade;}
else
{ grade = "Fail"; cout << grade;}
return 0;
}

November 3, 2021 Prof. Abdelaziz Khamis 19


Multiple Selections: Nested if
 Nesting: one control statement in another.
 An else is associated with the most recent if that has not
been paired with an else.
 For example:
if (score >= 90)
cout << "The grade is A“ << endl;
else if (score >= 80)
cout << "The grade is B“ << endl;
else if (score >= 70)
cout << "The grade is C“ << endl;
else
cout << "The grade is F“ << endl;

November 3, 2021 Prof. Abdelaziz Khamis 20


Conditional Operator (?:)
 Conditional operator (?:) takes three arguments (ternary)
 Syntax for using the conditional operator:
expression1 ? expression2 : expression3

If expression1 is true, the result of the conditional expression


is expression2. Otherwise, the result is expression3
 Consider the following statement:
if (a >= b)
max = a;
else
max = b;
You can use the conditional operator to simplify this statement
as follows:
max = (a >= b) ? a : b;

November 3, 2021 Prof. Abdelaziz Khamis 21


Programming Example
 A program that calculates a customer’s bill for a local
cable company. There are two types of customers:
residential and business. There are two rates for
calculating a cable bill: one for residential customers and
one for business customers.
 For residential customers, the following rates apply:
 Bill processing fee: $4.50
 Basic service fee: $20.50
 Premium channels: $7.50 per channel
 For business customers, the following rates apply:
 Bill processing fee: $15.00
 Basic service fee: $75.00 for first 10 connections, $5.00 for
each additional connection
 Premium channels: $50.00 per channel

November 3, 2021 Prof. Abdelaziz Khamis 22


Programming Example (Continued)
 The program should ask the user for an account number
(an integer) and a customer code. Assume that R or r
stands for a residential customer, and B or b stands for a
business customer.
 Problem Analysis:
 Input: int accountNumber;
char customerType;
int numOfPremChannels;
int numOfBasicServConn;
 Output: double amountDue;
 Named Constants:
const double RES_BILL_PROC_FEES = 4.50;
const double RES_BASIC_SERV_COST = 20.50;
const double RES_COST_PREM_CHANNEL = 7.50;

November 3, 2021 Prof. Abdelaziz Khamis 23


Programming Example (Continued)
 Named Constants: (continued)
const double BUS_BILL_PROC_FEES = 15.00;
const double BUS_BASIC_SERV_COST = 75.00;
const double BUS_BASIC_CONN_COST = 5.00;
const double BUS_COST_PREM_CHANNEL = 50.00;
 Formulas:
For a residential customer:
amountDue = RES_BILL_PROC_FEES + RES_BASIC_SERV_COST
+ numOfPremChannels * RES_COST_PREM_CHANNEL;
For a business customer:
if (numOfBasicServConn <= 10)
amountDue = BUS_BILL_PROC_FEES + BUS_BASIC_SERV_COST
+ numOfPremChannels * BUS_COST_PREM_CHANNEL;
else amountDue = BUS_BILL_PROC_FEES + BUS_BASIC_SERV_COST
+ (numOfBasicServConn – 10) * BUS_BASIC_CONN_COST
+ numOfPremChannels * BUS_COST_PREM_CHANNEL;

November 3, 2021 Prof. Abdelaziz Khamis 24


Programming Example (Continued)
 Algorithm Design:
1. Prompt the user for the account number and customer type.
2. If the customer type is R or r,
i. Prompt the user for the number of premium channels.
ii. Compute the bill.
iii. Print the bill.
3. If the customer type is B or b,
i. Prompt the user for the number of basic service connections
and number of premium channels.
ii. Compute the bill.
iii. Print the bill.

November 3, 2021 Prof. Abdelaziz Khamis 25

You might also like