0% found this document useful (0 votes)
80 views25 pages

Chapter 3 Part A

The document discusses selection control structures and logical operators in C++. It defines selection control structures as allowing a program to execute statements selectively or repetitively based on certain conditions being met. Relational operators like ==, !=, <, >, <=, >= allow comparisons, while logical operators like !, &&, || combine logical expressions and evaluate them. Examples are given of using relational and logical operators to check conditions like whether a year is a leap year. Common mistakes with operators are also discussed.

Uploaded by

Hadi Ida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views25 pages

Chapter 3 Part A

The document discusses selection control structures and logical operators in C++. It defines selection control structures as allowing a program to execute statements selectively or repetitively based on certain conditions being met. Relational operators like ==, !=, <, >, <=, >= allow comparisons, while logical operators like !, &&, || combine logical expressions and evaluate them. Examples are given of using relational and logical operators to check conditions like whether a year is a leap year. Common mistakes with operators are also discussed.

Uploaded by

Hadi Ida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

CHAPTER 3

SELECTION CONTROL STRUCTURE


(PART A)

You never will be the person you


can be if pressure, tension and
discipline are taken out of your life
~ Dr. James G. Bilkey ~

Control Structures
A computer can proceed:
in sequence
selectively (branch) - making a choice
repetitively (iteratively) - performing a
statement over and over, also called a loop
Some statements are executed only if certain
conditions are met
A condition is represented by a logical
(Boolean) expression that has a value of
either true or false
A condition is met if it evaluates to true

Relational Operators
A relational operator allows you to make
comparisons in a program
Equality of real numbers is usually machine
dependent so it is possible that on a
particular machine 6.8 + 3.1 == 2.7 + 7.2
might evaluate to false
Comparing values of different data types may
produce unpredictable results
For example, 8 < '5' should not be done
C++ returns an integer value of 1 if the
logical expression evaluates to true and the
value of 0 otherwise
In C++, any nonzero value is treated as true

Relational Operator
The following expressions are
true.
c == e
i != k
i<j
d>e
i <= k
j >= k

Example 2:
Given the following declarations:
int i = 1;
int j = 2;
int k = 2;
char c = 2;
char d = 3;
And the following expressions are
the false.
char e = 2;
i == j
c != e
j<k
c>e
d <= c
i >= k

Relational Operators in C++

Comparing string Types


Relational operators can be applied to string
variables
They are compared character by character, starting
with the first character, using the collating
sequence
This comparison continues until either a mismatch
is found or the last characters have been compared
and are 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

string Comparison Example


Suppose we have the following
declarations:
string str1 = "Hello";
string str2 = "Hi";
string str3 = "Air";
string str4 = "Bill";

Logical (Boolean) Operators


Logical (Boolean) operators enable you to combine
logical expressions
There are three logical (Boolean) operators:
! - not
&& and
|| - or
Logical operators take logical values as operands and
yield logical values as results
! is unary, so it has only one operand, while && and || are
binary operators
Putting ! in front of a logical expression reverses the
value of that logical expression

Truth Table for Operator !


p

!p

Example (assume age = 24, gender = 'M')

true

false

!(age > 18) is false, because (age > 18) is true.

false

true

!(gender != 'F') is true, because (grade != 'F') is false.

Truth Table for Operator


&&
p1

p2

p1 && p2

Example (assume age = 24, gender = 'F')

false

false

false

false

true

false

(age > 18) && (gender == 'F') is true, because (age


> 18) and (gender == 'F') are both true.

true

false

false

true

true

true

(age > 18) && (gender != 'F') is false, because


(gender != 'F') is false.

Truth Table for Operator ||


p1

p2

p1 || p2

Example (assume age = 24, gender = 'F')

false

false

false

false

true

true

(age > 34) || (gender == 'F') is true, because (gender


== 'F') is true.

true

false

true

true

true

true

(age > 34) || (gender == 'M') is false, because (age >


34) and (gender == 'M') are both false.

Short-Circuit Operator
When evaluating p1 && p2, C++ first evaluates p1 and then
evaluates p2 if p1 is true; if p1 is false, it does not evaluate p2.
When evaluating p1 || p2, C++ first evaluates p1 and then
evaluates p2 if p1 is false; if p1 is true, it does not evaluate p2.
Therefore, && is referred to as the conditional or short-circuit
AND operator, and || is referred to as the conditional or shortcircuit OR operator.

Examples
Write a program that lets the user enter a year and checks whether
it is a leap year.
A year is a leap year if it is divisible by 4 but not by 100 or if it is
divisible by 400. So you can use the following Boolean
expression to check whether a year is a leap year:
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)

Precedence of Operators
Relational and logical operators are evaluated
from left to right so the associativity is said to be
from left to right
Parentheses can be used to indicate an order of
operation that is different than the order
indicated by order of precedence
Short-circuit evaluation (of a logical expression) a process in which the computer evaluates a
logical expression from left to right and stops as
soon as the value of the expression is known

Logical Expression
Example 1:
(gender == m) && (age > 20)
Example 2:
5 * 15 + 4 == 13 && 12 < 19 || !false
== 5 < 24
It is equivalent to:
((((5 *15) + 4) == 13) && (12 < 19))||((!
false) == (5 < 24))

Troubleshooting
First mistake:
suppose a programmer needs to check if x
is greater than
both y and z
x > y && z
operation
(x > y && x > z)

// illegal
// correct way

Troubleshooting
Second mistake:
the difference between relational operator,
= = and assignment operator, =
a = b
a == b
and b

// assign b into a
// check to see if a
// have same value

Troubleshooting
Third mistake:
suppose a programmer needs to check if A
or B is true and if C or D is true
( A || B && C || D)
((A || B) && (C || D))
way

// illegal
// correct

How Good is Your


Knowledge??
Give the value TRUE or FALSE for the following
simple Boolean expressions:
assume that i = 4 and j = 6
Boolean Expression
2==3
i==4
j>=5
j!=i
i<j+1

Value

How Good is Your


Knowledge??
Give the value TRUE or FALSE for the following
compound Boolean expressions:
assume that x = 5, y = 6 and z = 7
Boolean Expression
x < y && z > y
x < = z && x > 0
x > = y && z = = 3
y < 15 || !y > 0
x > y || z > y && !z <
=0

Value

You might also like