SlideShare a Scribd company logo
1 of 25
Object Oriented Programming: 16
Boolean Expressions and Selection
Prof Neeraj Bhargava
Vaibhav Khanna
Department of Computer Science
School of Engineering and Systems Sciences
Maharshi Dayanand Saraswati University Ajmer
2
The bool Data Type and Logical
(Boolean) Expressions
• The data type bool has 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
3
Logical (Boolean) Expressions
• Logical expressions can be unpredictable
• The following expression appears to represent a
comparison of 0, num, and 10:
0 <= num <= 10
• It always evaluates to true because 0 <= num
evaluates to either 0 or 1, and 0 <= 10 is
true and 1 <= 10 is true
• A correct way to write this expression is:
0 <= num && num <= 10
4
Selection: if and if...else
• One-Way Selection
• Two-Way Selection
• Compound (Block of) Statements
• Multiple Selections: Nested if
• Comparing if...else Statements with a
Series of if Statements
5
Selection: if and if...else
(continued)
• Using Pseudocode to Develop, Test, and
Debug a Program
• Input Failure and the if Statement
• Confusion Between the Equality Operator (==)
and the Assignment Operator (=)
• Conditional Operator (?:)
6
One-Way Selection
• The syntax of one-way selection is:
• The statement is executed if the value of the
expression is true
• The statement is bypassed if the value is
false; program goes to the next statement
• if is a reserved word
7
One-Way Selection (continued)
8
One-Way Selection (continued)
10
One-Way Selection (continued)
11
Two-Way Selection
• Two-way selection takes the form:
• If expression is true, statement1 is
executed; otherwise, statement2 is
executed
– statement1 and statement2 are any C++
statements
• else is a reserved word
12
Two-Way Selection (continued)
13
Two-Way Selection (continued)
14
Two-Way Selection (continued)
15
Compound (Block of) Statement
• Compound statement (block of statements):
• A compound statement is a single statement
16
Compound (Block of) Statement
(continued)
if (age > 18)
{
cout << "Eligible to vote." << endl;
cout << "No longer a minor." << endl;
}
else
{
cout << "Not eligible to vote." << endl;
cout << "Still a minor." << endl;
}
17
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
19
Multiple Selections: Nested if
(continued)
20
Comparing if…else Statements with
a Series of if Statements
21
Using Pseudocode to Develop, Test,
and Debug a Program
• Pseudocode (pseudo): provides a useful
means to outline and refine a program before
putting it into formal C++ code
• You must first develop a program using paper
and pencil
• On paper, it is easier to spot errors and
improve the program
– Especially with large programs
22
Input Failure and the if Statement
• If input stream enters a fail state
– All subsequent input statements associated with
that stream are ignored
– Program continues to execute
– May produce erroneous results
• Can use if statements to check status of
input stream
• If stream enters the fail state, include
instructions that stop program execution
23
Confusion Between == and =
• C++ allows you to use any expression that can
be evaluated to either true or false as an
expression in the if statement:
if (x = 5)
cout << "The value is five." << endl;
• The appearance of = in place of == resembles
a silent killer
– It is not a syntax error
– It is a logical error
24
Conditional Operator (?:)
• Conditional operator (?:) takes three
arguments
– Ternary operator
• 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
Assignment
• Explain the usage of one way selection IF
Statement in C++
• Explain the usage of two way selection IF Else
Statement in C++

More Related Content

What's hot

Data types and operators in vb
Data types and operators  in vbData types and operators  in vb
Data types and operators in vb
alldesign
 
Decision statements in vb.net
Decision statements in vb.netDecision statements in vb.net
Decision statements in vb.net
ilakkiya
 
EquivalencePartition
EquivalencePartitionEquivalencePartition
EquivalencePartition
swornim nepal
 

What's hot (20)

Ch04
Ch04Ch04
Ch04
 
Ch7 Basic Types
Ch7 Basic TypesCh7 Basic Types
Ch7 Basic Types
 
Type Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityType Conversion, Precedence and Associativity
Type Conversion, Precedence and Associativity
 
Tech tut
Tech tutTech tut
Tech tut
 
Equivalence partitioning
Equivalence partitioningEquivalence partitioning
Equivalence partitioning
 
Second session from learn python tutorial
Second session from learn python tutorialSecond session from learn python tutorial
Second session from learn python tutorial
 
Type conversion, precedence, associativity in c programming
Type conversion, precedence, associativity in c programmingType conversion, precedence, associativity in c programming
Type conversion, precedence, associativity in c programming
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Ch03
Ch03Ch03
Ch03
 
Data types and operators in vb
Data types and operators  in vbData types and operators  in vb
Data types and operators in vb
 
Decision statements in vb.net
Decision statements in vb.netDecision statements in vb.net
Decision statements in vb.net
 
EquivalencePartition
EquivalencePartitionEquivalencePartition
EquivalencePartition
 
Finite state system or finite automata
Finite state system or finite automataFinite state system or finite automata
Finite state system or finite automata
 
L01 intro-daa - ppt1
L01 intro-daa - ppt1L01 intro-daa - ppt1
L01 intro-daa - ppt1
 
Python Programming | JNTUK | UNIT 1 | Lecture 5
Python Programming | JNTUK | UNIT 1 | Lecture 5Python Programming | JNTUK | UNIT 1 | Lecture 5
Python Programming | JNTUK | UNIT 1 | Lecture 5
 
Equivalence partitions analysis
Equivalence partitions analysisEquivalence partitions analysis
Equivalence partitions analysis
 
C# conditional branching statement
C# conditional branching statementC# conditional branching statement
C# conditional branching statement
 
"A short and knowledgeable concept about Algorithm "
"A short and knowledgeable concept about Algorithm ""A short and knowledgeable concept about Algorithm "
"A short and knowledgeable concept about Algorithm "
 
Control Structures: Part 1
Control Structures: Part 1Control Structures: Part 1
Control Structures: Part 1
 
Vb decision making statements
Vb decision making statementsVb decision making statements
Vb decision making statements
 

Similar to Object oriented programming16 boolean expressions and selection statements

ESCM303 Introduction to Python Programming.pptx
ESCM303 Introduction to Python Programming.pptxESCM303 Introduction to Python Programming.pptx
ESCM303 Introduction to Python Programming.pptx
AvijitChaudhuri3
 
IF & SWITCH (conditional control) in computer science
IF & SWITCH (conditional control) in computer scienceIF & SWITCH (conditional control) in computer science
IF & SWITCH (conditional control) in computer science
RaianaTabitha
 

Similar to Object oriented programming16 boolean expressions and selection statements (20)

Selection
SelectionSelection
Selection
 
Control structures selection
Control structures   selectionControl structures   selection
Control structures selection
 
CSC111-Chap_03.pdf
CSC111-Chap_03.pdfCSC111-Chap_03.pdf
CSC111-Chap_03.pdf
 
Ch5 Selection Statements
Ch5 Selection StatementsCh5 Selection Statements
Ch5 Selection Statements
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
Control Structures, If..else, switch..case.pptx
Control Structures, If..else, switch..case.pptxControl Structures, If..else, switch..case.pptx
Control Structures, If..else, switch..case.pptx
 
Ch05.pdf
Ch05.pdfCh05.pdf
Ch05.pdf
 
CIS 1403 lab 4 selection
CIS 1403 lab 4 selectionCIS 1403 lab 4 selection
CIS 1403 lab 4 selection
 
C++ problem solving operators ( conditional operators,logical operators, swit...
C++ problem solving operators ( conditional operators,logical operators, swit...C++ problem solving operators ( conditional operators,logical operators, swit...
C++ problem solving operators ( conditional operators,logical operators, swit...
 
Control structures i
Control structures i Control structures i
Control structures i
 
Using decision statements
Using decision statementsUsing decision statements
Using decision statements
 
Chapter 4 5
Chapter 4 5Chapter 4 5
Chapter 4 5
 
ESCM303 Introduction to Python Programming.pptx
ESCM303 Introduction to Python Programming.pptxESCM303 Introduction to Python Programming.pptx
ESCM303 Introduction to Python Programming.pptx
 
Chaptfffffuuer05.PPT
Chaptfffffuuer05.PPTChaptfffffuuer05.PPT
Chaptfffffuuer05.PPT
 
Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)
 
Ch05-converted.pptx
Ch05-converted.pptxCh05-converted.pptx
Ch05-converted.pptx
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptx
 
Object oriented programming15 control structures relational operators
Object oriented programming15 control structures relational operatorsObject oriented programming15 control structures relational operators
Object oriented programming15 control structures relational operators
 
IF & SWITCH (conditional control) in computer science
IF & SWITCH (conditional control) in computer scienceIF & SWITCH (conditional control) in computer science
IF & SWITCH (conditional control) in computer science
 
4. decision making and some basic problem
4. decision making and some basic problem4. decision making and some basic problem
4. decision making and some basic problem
 

More from Vaibhav Khanna

More from Vaibhav Khanna (20)

Information and network security 47 authentication applications
Information and network security 47 authentication applicationsInformation and network security 47 authentication applications
Information and network security 47 authentication applications
 
Information and network security 46 digital signature algorithm
Information and network security 46 digital signature algorithmInformation and network security 46 digital signature algorithm
Information and network security 46 digital signature algorithm
 
Information and network security 45 digital signature standard
Information and network security 45 digital signature standardInformation and network security 45 digital signature standard
Information and network security 45 digital signature standard
 
Information and network security 44 direct digital signatures
Information and network security 44 direct digital signaturesInformation and network security 44 direct digital signatures
Information and network security 44 direct digital signatures
 
Information and network security 43 digital signatures
Information and network security 43 digital signaturesInformation and network security 43 digital signatures
Information and network security 43 digital signatures
 
Information and network security 42 security of message authentication code
Information and network security 42 security of message authentication codeInformation and network security 42 security of message authentication code
Information and network security 42 security of message authentication code
 
Information and network security 41 message authentication code
Information and network security 41 message authentication codeInformation and network security 41 message authentication code
Information and network security 41 message authentication code
 
Information and network security 40 sha3 secure hash algorithm
Information and network security 40 sha3 secure hash algorithmInformation and network security 40 sha3 secure hash algorithm
Information and network security 40 sha3 secure hash algorithm
 
Information and network security 39 secure hash algorithm
Information and network security 39 secure hash algorithmInformation and network security 39 secure hash algorithm
Information and network security 39 secure hash algorithm
 
Information and network security 38 birthday attacks and security of hash fun...
Information and network security 38 birthday attacks and security of hash fun...Information and network security 38 birthday attacks and security of hash fun...
Information and network security 38 birthday attacks and security of hash fun...
 
Information and network security 37 hash functions and message authentication
Information and network security 37 hash functions and message authenticationInformation and network security 37 hash functions and message authentication
Information and network security 37 hash functions and message authentication
 
Information and network security 35 the chinese remainder theorem
Information and network security 35 the chinese remainder theoremInformation and network security 35 the chinese remainder theorem
Information and network security 35 the chinese remainder theorem
 
Information and network security 34 primality
Information and network security 34 primalityInformation and network security 34 primality
Information and network security 34 primality
 
Information and network security 33 rsa algorithm
Information and network security 33 rsa algorithmInformation and network security 33 rsa algorithm
Information and network security 33 rsa algorithm
 
Information and network security 32 principles of public key cryptosystems
Information and network security 32 principles of public key cryptosystemsInformation and network security 32 principles of public key cryptosystems
Information and network security 32 principles of public key cryptosystems
 
Information and network security 31 public key cryptography
Information and network security 31 public key cryptographyInformation and network security 31 public key cryptography
Information and network security 31 public key cryptography
 
Information and network security 30 random numbers
Information and network security 30 random numbersInformation and network security 30 random numbers
Information and network security 30 random numbers
 
Information and network security 29 international data encryption algorithm
Information and network security 29 international data encryption algorithmInformation and network security 29 international data encryption algorithm
Information and network security 29 international data encryption algorithm
 
Information and network security 28 blowfish
Information and network security 28 blowfishInformation and network security 28 blowfish
Information and network security 28 blowfish
 
Information and network security 27 triple des
Information and network security 27 triple desInformation and network security 27 triple des
Information and network security 27 triple des
 

Recently uploaded

Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Recently uploaded (20)

WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - Kanchana
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AI
 

Object oriented programming16 boolean expressions and selection statements

  • 1. Object Oriented Programming: 16 Boolean Expressions and Selection Prof Neeraj Bhargava Vaibhav Khanna Department of Computer Science School of Engineering and Systems Sciences Maharshi Dayanand Saraswati University Ajmer
  • 2. 2 The bool Data Type and Logical (Boolean) Expressions • The data type bool has 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
  • 3. 3 Logical (Boolean) Expressions • Logical expressions can be unpredictable • The following expression appears to represent a comparison of 0, num, and 10: 0 <= num <= 10 • It always evaluates to true because 0 <= num evaluates to either 0 or 1, and 0 <= 10 is true and 1 <= 10 is true • A correct way to write this expression is: 0 <= num && num <= 10
  • 4. 4 Selection: if and if...else • One-Way Selection • Two-Way Selection • Compound (Block of) Statements • Multiple Selections: Nested if • Comparing if...else Statements with a Series of if Statements
  • 5. 5 Selection: if and if...else (continued) • Using Pseudocode to Develop, Test, and Debug a Program • Input Failure and the if Statement • Confusion Between the Equality Operator (==) and the Assignment Operator (=) • Conditional Operator (?:)
  • 6. 6 One-Way Selection • The syntax of one-way selection is: • The statement is executed if the value of the expression is true • The statement is bypassed if the value is false; program goes to the next statement • if is a reserved word
  • 9.
  • 11. 11 Two-Way Selection • Two-way selection takes the form: • If expression is true, statement1 is executed; otherwise, statement2 is executed – statement1 and statement2 are any C++ statements • else is a reserved word
  • 15. 15 Compound (Block of) Statement • Compound statement (block of statements): • A compound statement is a single statement
  • 16. 16 Compound (Block of) Statement (continued) if (age > 18) { cout << "Eligible to vote." << endl; cout << "No longer a minor." << endl; } else { cout << "Not eligible to vote." << endl; cout << "Still a minor." << endl; }
  • 17. 17 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
  • 18.
  • 20. 20 Comparing if…else Statements with a Series of if Statements
  • 21. 21 Using Pseudocode to Develop, Test, and Debug a Program • Pseudocode (pseudo): provides a useful means to outline and refine a program before putting it into formal C++ code • You must first develop a program using paper and pencil • On paper, it is easier to spot errors and improve the program – Especially with large programs
  • 22. 22 Input Failure and the if Statement • If input stream enters a fail state – All subsequent input statements associated with that stream are ignored – Program continues to execute – May produce erroneous results • Can use if statements to check status of input stream • If stream enters the fail state, include instructions that stop program execution
  • 23. 23 Confusion Between == and = • C++ allows you to use any expression that can be evaluated to either true or false as an expression in the if statement: if (x = 5) cout << "The value is five." << endl; • The appearance of = in place of == resembles a silent killer – It is not a syntax error – It is a logical error
  • 24. 24 Conditional Operator (?:) • Conditional operator (?:) takes three arguments – Ternary operator • 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
  • 25. Assignment • Explain the usage of one way selection IF Statement in C++ • Explain the usage of two way selection IF Else Statement in C++