Lesson 4 COMP218
Lesson 4 COMP218
Lesson 4
Selection statement allows the program to make a choice, that is to perform a test and
C++
==
!=
<
<
<=
>
>
>=
Lesson 4
Lesson 4
C++
NOT
AND
&&
OR
||
Priority
1
2
3
&&
Logical AND
||
Logical OR
Lesson 4
Slide 7: Statements
Logical Statements
Example 1:
0 x 1 is written
0 <= x && x <= 1
0.0 <= x && x <= 1.0
Example 2:
The following are equivalent
!(age < 18) && !(age > 65)
age >= 18 && age <= 65
!(age < 18 || age > 65)
Slide 8: Statements
Logical Statements (cont'd)
Write logical expressions that correspond to each phrase:
1) integers a, b, c are all even.
2) at least one of integers a, b, c is even.
3) neither integer m nor n are odd.
4) both real number x and y are positive.
Check video for answers.
Lesson 4
9: Statem
ments
al Statements (con
nt'd)
gical expresssions that correspond
d to each phrase:
a digit charracter (i.e. ch is '0', '1'', ..., '9').
not a lowerr-case charracter.
xactly one of
o the real numbers
n
x and
a y is po
ositive.
10: Flowc
chart
hart is a gra
aphical way
y of represe
enting an allgorithm.
he following symbols:
11: Flowc
chart
le:
e and write
e out to scre
een the ave
erage of 3 g
grades. Pro
ompt user ffor 3 gradess.
video fo
or answer
r.
if (condition)
action statement
Example 1:
if (age >= 18) cout << "may vote";
Example 2:
if (month == 4)
days_in_month = 30;
Lesson 4
13: One-W
Way (If) Selection
n
hart
ndition is first
f
tested.
esult is true, statemen
nt
f the loop) is executed
d;
se, program
m control pa
asses to the
e
atement.
dition is giv
ven as a log
gical
on and must be includ
ded in
eses.
14: One-W
Way (If) Selection
n
le 3:
+ y > 8) cout <<
< count +=
+ 1;
< count;
n x = 5, y = 2 and cou
unt = 31, what
w
will be
?
n x = 5, y = 12 and co
ount = 31, what
w
will bee
?
or answer
rs.
video fo
if (condition)
{
action statement 1
action statement 2
...
action statement n
}
Lesson 4
17: One-W
Way (If) Selection
n
ed One-W
Way Selec
ction (con
nt'd)
le 1b)
er please
e: ";
< "Numbe
> num;
um > 100)
ut << "T
That is a big num
mber!";
um = 0;
18: One-W
Way (If) Selection
n
ed One-W
Way Selec
ction (con
nt'd)
ng content of
o two varia
ables. How??
10
0
Form:
{
statement 1
statement 2
...
statement n
}
Lesson 4
11
21: Two-W
Way (If...Else) Se
election
dition)
entT
entF
:
ondition is met, action
n statementtT is executted, otherw
wise
entF is executed.
22: Two-W
Way (If...Else) Se
election
le 1:
% 2 == 0)
<< n << " is eve
en" << en
ndl;
d" << end
dl;
<< n << " is odd
12
2
Lesson 4
13
Lesson 4
14
There are situations where decisions are made on more than two alternatives.
Multiple selection combines the "else" and following "if " condition on a single line.
Lesson 4
15
Lesson 4
16
Lesson 4
17
Lesson 4
18
Hours
-5
Rate
8.50
Lesson 4
19
Hours
50
Rate
100
Lesson 4
20
Hours
10
Rate
3.50
Letter
100 - 90
89 - 80
79 - 70
69 - 0
B
C
F
Write a nested if statement that takes the number grade and outputs the equivalent
letter grade.
Lesson 4
21
Letter
A
B
C
F
>= 65)
<< "You can retire!";
< 65)
<< "Keep working";
Lesson 4
22
Lesson 4
23
n is 1:
n is 3:
n is 4:
n is 7:
all other values of n: m is 0
m is n
m is n*n
m is n*n*n
m is 1
A nested if statement begins with an outer control structure that is either a simple if
or an if/else statement.
Within the outer if statement are sub-statements that are themselves simple if or
if/else statements.
While indenting is important for program readability, it is not used by the compiler to
match the if and else statements.
Lesson 4
24
AccBalance IsSenior
$200.00
$200.00
$100.00
$100.00
true
false
true
false
Lesson 4
25
Lesson 4
26
Lesson 4
27
The selector expression and the constants must be of the same ordinal type. This
includes integers, character, bool (and enumerated types).
The break statement is necessary to tell the system to "break" out of the switch
statement.
You need not have a default section. If no matches are found, then nothing happens
when the switch statement is executed.
Lesson 4
28
Lesson 4
29
Assign value .25 to discountRate and assign value 10.00 to shipCost if purchase is
over 100.00;
Otherwise, assign value .15 to discountRate and assign value 5.00 to shipCost;
Lesson 4
30
Lesson 4
31
int age;
age = 30;
if ( age < 18 )
cout << "Do you drive?";
cout << "Too young to vote";
35 through 60 as "Unpleasant"
32
Declaration/initialization:
bool valid;
bool isOver18 = true
Lesson 4
33
Lesson 4
34