SlideShare a Scribd company logo
‫ا‬ ِ
‫ن‬ ٰ‫م‬ ْ
‫ح‬َّ
‫الر‬ ِ‫هللا‬ ِ
‫م‬ ْ
‫س‬ِ‫ب‬
ِ
‫م‬ْ‫ي‬ ِ
‫ح‬َّ
‫لر‬
1
Chapter 4:
Making
Decisions
2
4.1
Relational Operators
3
Relational Operators
• Used to compare numbers to determine
relative order
• Operators:
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
== Equal to
!= Not equal to
4
Relational Expressions
• Boolean expressions – true or false
• Examples:
12 > 5 is true
7 <= 5 is false
if x is 10, then
x == 10 is true,
x != 8 is true, and
x == 8 is false
5
Relational Expressions
• Can be assigned to a variable:
result = x <= y;
• Assigns 0 for false, 1 for true
• Do not confuse = and ==
6
4.2
The if Statement
7
The if Statement
• Allows statements to be conditionally
executed or skipped over
• Models the way we mentally evaluate
situations:
– "If it is raining, take an umbrella."
– "If it is cold outside, wear a coat."
8
Flowchart for Evaluating a Decision
9
The if Statement
• General Format:
if (expression)
statement;
10
The if Statement-What Happens
To evaluate:
if (expression)
statement;
• If the expression is true, then
statement is executed.
• If the expression is false, then
statement is skipped.
11
if Statement in Program 4-2
Continued…
12
if Statement in Program 4-2
13
Flowchart for Program 4-2 Lines 21
and 22
14
if Statement Notes
• Do not place ; after (expression)
• Place statement; on a separate line
after (expression), indented:
if (score > 90)
grade = 'A';
• Be careful testing floats and doubles
for equality
• 0 is false; any other value is true
15
4.3
Expanding the if Statement
16
Expanding the if Statement
• To execute more than one statement as part of
an if statement, enclose them in { }:
if (score > 90)
{
grade = 'A';
cout << "Good Job!n";
}
• { } creates a block of code
17
4.4
The if/else Statement
18
The if/else statement
• Provides two possible paths of execution
• Performs one statement or block if the
expression is true, otherwise performs
another statement or block.
• General Format:
if (expression)
statement1; // or block
else
statement2; // or block
19
if/else-What Happens
To evaluate:
if (expression)
statement1;
else
statement2;
• If the expression is true, then statement1 is
executed and statement2 is skipped.
• If the expression is false, then statement1 is
skipped and statement2 is executed.
20
if/else-What Happens
21
The if/else statement and
Modulus Operator in Program 4-8
22
Flowchart for Program 4-8 Lines 14
through 18
23
Testing the Divisor in Program 4-9
Continued…
24
Testing the Divisor in Program 4-9
25
The if/else if/else statement
• General Format:
if (expression)
statement1; // or block1
else if (expression)
statement2; // or block2
else
statement3; // or block2
26
The if/else if/else statement
27
The if/else if/else statement
28
4.5
Nested if Statements
29
Nested if Statements
• An if statement that is nested inside
another if statement
• Nested if statements can be used to test
more than one condition
30
Flowchart for a Nested if
Statement
31
Nested if Statements
32
Nested if Statements
33
Use Proper Indentation!
34
4.6
The if/else if Statement
35
The if/else if Statement
• Tests a series of conditions until one is found to
be true
• Often simpler than using nested if/else
statements
• Can be used to model thought processes such
as:
"If it is raining, take an umbrella,
else, if it is windy, take a hat,
else, take sunglasses”
36
if/else if Format
if (expression)
statement1; // or block
else if (expression)
statement2; // or block
.
. // other else ifs
.
else if (expression)
statementn; // or block
37
The if/else if Statement
38
Using a Trailing else to Catch
Errors
• The trailing else clause is optional, but it
is best used to catch errors.
This trailing
else
catches
invalid test
scores
39
4.7
Flags
40
Flags
• Variable that signals a condition
• Usually implemented as a bool variable
• Can also be an integer
– The value 0 is considered false
– Any nonzero value is considered true
• As with other variables in functions, must
be assigned an initial value before it is
used
41
4.8
Logical Operators
42
Logical Operators
• Used to create relational expressions from
other relational expressions
• Operators, meaning, and explanation:
&& AND New relational expression is true if both
expressions are true
|| OR New relational expression is true if either
expression is true
! NOT Reverses the value of an expression – true
expression becomes false, and false becomes
true
43
Logical Operators-Examples
int x = 12, y = 5, z = -4;
(x > y) && (y > z) true
(x > y) && (z > y) false
(x <= z) || (y == z) false
(x <= z) || (y != z) true
!(x >= z) false
44
The logical && operator
45
The logical || Operator
46
The logical! Operator
47
Checking Numeric Ranges with
Logical Operators
• Used to test to see if a value falls inside a range:
if (grade >= 0 && grade <= 100)
cout << "Valid grade";
• Can also test to see if value falls outside of range:
if (grade <= 0 || grade >= 100)
cout << "Invalid grade";
• Cannot use mathematical notation:
if (0 <= grade <= 100) //doesn’t work!
48
4.9
The Conditional Operator
49
The Conditional Operator
• Can use to create short if/else
statements
• Format: expr ? expr : expr;
x<0 ? y=10 : z=20;
First Expression:
Expression to be
tested
2nd Expression:
Executes if first
expression is true
3rd Expression:
Executes if the first
expression is false
50
The Conditional Operator
• The value of a conditional expression is
– The value of the second expression if the first
expression is true
– The value of the third expression if the first
expression is false
• Parentheses () may be needed in an
expression due to precedence of
conditional operator
51
The Conditional Operator
52
4.10
The switch Statement
53
The switch Statement
• Used to select among statements from
several alternatives
• In some cases, can be used instead of
if/else if statements
54
switch Statement Format
switch (expression) //integer
{
case exp1: statement1;
case exp2: statement2;
...
case expn: statementn;
default: statementn+1;
}
55
switch Statement Format
56
The switch Statement
57
switch Statement Requirements
1) expression must be an integer variable
or an expression that evaluates to an
integer value
2) exp1 through expn must be constant
integer expressions or literals, and must
be unique in the switch statement
3) default is optional but recommended
58
switch Statement-How it Works
1) expression is evaluated
2) The value of expression is compared
against exp1 through expn.
3) If expression matches value expi, the
program branches to the statement
following expi and continues to the end
of the switch
4) If no matching value is found, the
program branches to the statement after
default:
59
break Statement
• Used to exit a switch statement
• If it is left out, the program "falls through"
the remaining statements in the switch
statement
60
break Statement – Not Used
61
break Statement - Used
62
break and default statements
Continued…
63
break and default statements in
Program 4-25
64
Using switch in Menu Systems
• switch statement is a natural choice for
menu-driven program:
– display the menu
– then, get the user's menu selection
– use user input as expression in switch
statement
– use menu choices as expr in case
statements
65
JAZAK ALLAH!
Any Question?

More Related Content

PPT
Chapter 4 Making Decisions
GhulamHussain142878
 
PPT
Chaptfffffuuer05.PPT
sdvdsvsdvsvds
 
PPTX
C++ IF STATMENT AND ITS TYPE
UNIVERSITY OF ENGINEERING AND TECHNOLOGY TAXILA
 
PDF
Selection & Making Decisions in c
yndaravind
 
PPTX
IF Statement
Yunis20
 
PPTX
Decision structures chpt_5
cmontanez
 
PDF
Fundamentals of Computer Programming - Flow of Control I
ChereLemma2
 
PPT
CHAPTER-3a.ppt
Tekle12
 
Chapter 4 Making Decisions
GhulamHussain142878
 
Chaptfffffuuer05.PPT
sdvdsvsdvsvds
 
Selection & Making Decisions in c
yndaravind
 
IF Statement
Yunis20
 
Decision structures chpt_5
cmontanez
 
Fundamentals of Computer Programming - Flow of Control I
ChereLemma2
 
CHAPTER-3a.ppt
Tekle12
 

Similar to C++ problem solving operators ( conditional operators,logical operators, switch statements) (20)

PDF
ICP - Lecture 7 and 8
Hassaan Rahman
 
PPT
Java Programmin: Selections
Karwan Mustafa Kareem
 
PPT
Ch3
aamirsahito
 
PPT
Eo gaddis java_chapter_04_5e
Gina Bullock
 
PPT
Eo gaddis java_chapter_04_5e
Gina Bullock
 
DOCX
Copyright © 2018 Pearson Education, Inc.C H A P T E R 3.docx
dickonsondorris
 
PPT
chap04-conditional.ppt
HeshamMohamed855920
 
PPTX
unit2 C-ProgrammingChapter 2 Control statements.pptx
JavvajiVenkat
 
PPTX
Programming note C#
Ahmad Syahmi Irfan
 
PPTX
Pi j1.3 operators
mcollison
 
PPTX
03_Gaddis Python_Lecture_ppt_ch03.pptx
ssuser58efc81
 
PPTX
Module2.2_Operators-in-C-Programming.pptx
MaheshKini3
 
PPTX
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
EG20910848921ISAACDU
 
PPT
6 programming-using-java decision-making20102011-
Mahmoud Alfarra
 
PPTX
ICSE Class X Conditional Statements in java
VanitaKarthik2
 
PDF
Decision control
Learn By Watch
 
PPT
Control statments in c
CGC Technical campus,Mohali
 
PPT
L05if
Kgr Sushmitha
 
PPT
Ppt lesson 08
Linda Bodrie
 
ICP - Lecture 7 and 8
Hassaan Rahman
 
Java Programmin: Selections
Karwan Mustafa Kareem
 
Eo gaddis java_chapter_04_5e
Gina Bullock
 
Eo gaddis java_chapter_04_5e
Gina Bullock
 
Copyright © 2018 Pearson Education, Inc.C H A P T E R 3.docx
dickonsondorris
 
chap04-conditional.ppt
HeshamMohamed855920
 
unit2 C-ProgrammingChapter 2 Control statements.pptx
JavvajiVenkat
 
Programming note C#
Ahmad Syahmi Irfan
 
Pi j1.3 operators
mcollison
 
03_Gaddis Python_Lecture_ppt_ch03.pptx
ssuser58efc81
 
Module2.2_Operators-in-C-Programming.pptx
MaheshKini3
 
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
EG20910848921ISAACDU
 
6 programming-using-java decision-making20102011-
Mahmoud Alfarra
 
ICSE Class X Conditional Statements in java
VanitaKarthik2
 
Decision control
Learn By Watch
 
Control statments in c
CGC Technical campus,Mohali
 
Ppt lesson 08
Linda Bodrie
 
Ad

Recently uploaded (20)

PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
Architecture of the Future (09152021)
EdwardMeyman
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Software Development Company | KodekX
KodekX
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
Architecture of the Future (09152021)
EdwardMeyman
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Software Development Company | KodekX
KodekX
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Ad

C++ problem solving operators ( conditional operators,logical operators, switch statements)