SlideShare a Scribd company logo
1 of 35
CONTROL STRUCTURES (SELECTION)
Objectives
In this chapter, you will:
• Learn about control structures
• Examine relational and logical operators
• Explore how to form and evaluate logical
  (Boolean) expressions
• Discover how to use the selection control
  structures if, if...else, and switch in a
  program
                                              2
Control Structures
• A computer can proceed:
  – In sequence
  – Selectively (branch) - making a choice
  – Repetitively (iteratively) - looping
• Some statements are executed only if certain
  conditions are met
• A condition is met if it evaluates to true


                                                 3
Control Structures (cont.)




                             4
Relational Operators
• A condition is represented by a logical
  (Boolean) expression that can be true or
  false
• Relational operators:
  – Allow comparisons
  – Require two operands (binary)
  – Evaluate to true or false



                                             5
Relational Operators (cont.)




                               6
Relational Operators (cont.)
• You can use the relational operators with all
  three simple data types:
  – 8 < 15 evaluates to true
  – 6 != 6 evaluates to false
  – 2.5 > 5.8 evaluates to false
  – 5.9 <= 7.5 evaluates to true




                                                  7
Relational Operators (cont.)




                               8
Logical (Boolean) Operators and
          Logical Expressions
• Logical (Boolean) operators enable you to
  combine logical expressions


           unary

           binary

           binary




                                              9
Logical (Boolean) Operators and
   Logical Expressions (cont.)




                                  10
11
12
Order of Precedence

• Relational and logical operators are
  evaluated from left to right
• The associativity is left to right
• Parentheses can override precedence




                                         13
Order of Precedence (cont.)




                              14
Order of Precedence (cont.)




                              15
Order of Precedence (cont.)




                              16
Order of Precedence (cont.)




                              17
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



                                            18
Selection: if and if...else (cont.)
• 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

                                                     19
One-Way Selection (cont.)




                            20
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


                                                    21
Two-Way Selection (cont.)




                            22
Two-Way Selection (cont.)




                            23
Compound (Block of) Statement
• Compound statement (block of statements):




• A compound statement is a single statement

                                               24
Compound (Block of) Statement
(cont.)
 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;
 }
                                            25
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




                                                       26
27
Comparing if…else Statements with
     a Series of if Statements




                                28
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

                                                 29
switch Structures
• switch structure: alternate
  to if-else
• switch (integral) expression
  is evaluated first
• Value of the expression
  determines which
  corresponding action is taken
• Expression is sometimes
  called the selector


                                  30
31
switch Structures (cont.)
• One or more statements may follow a case
  label
• Braces are not needed to turn multiple
  statements into a single compound statement
• The break statement may or may not appear
  after each statement
• switch, case, break, and default are
  reserved words


                                            32
33
Summary
• Control structures alter normal control flow
• Most common control structures are selection and
  repetition
• Relational operators: ==, <, <=, >, >=, !=
• Logical expressions evaluate to 1 (true) or 0
  (false)
• Logical operators: ! (not), && (and), || (or)
• Two selection structures: one-way selection and
  two-way selection
• The expression in an if or if...else structure is
  usually a logical expression

                                                      34
Summary (continued)
• No stand-alone else statement in C++
   – Every else has a related if
• A sequence of statements enclosed between braces,
  { and }, is called a compound statement or block of
  statements
• Using assignment in place of the equality operator
  creates a semantic error
• switch structure handles multiway selection
• break statement ends switch statement
 Source:
 C++ Programming: From Problem Analysis to Program Design,
 Fourth Edition                                              35

More Related Content

What's hot

Control structure C++
Control structure C++Control structure C++
Control structure C++Anil Kumar
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in JavaNiloy Saha
 
If and select statement
If and select statementIf and select statement
If and select statementRahul Sharma
 
Conditional statements
Conditional statementsConditional statements
Conditional statementscherrybear2014
 
Chapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsChapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsIt Academy
 
Lecture 3 Conditionals, expressions and Variables
Lecture 3   Conditionals, expressions and VariablesLecture 3   Conditionals, expressions and Variables
Lecture 3 Conditionals, expressions and VariablesSyed Afaq Shah MACS CP
 
Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in JavaJin Castor
 
Selection statements
Selection statementsSelection statements
Selection statementsHarsh Dabas
 
Control statements in java
Control statements in javaControl statements in java
Control statements in javaManojkumar C
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statementsKuppusamy P
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision ControlJayfee Ramos
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__elseeShikshak
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C ProgrammingKamal Acharya
 
Object oriented programming16 boolean expressions and selection statements
Object oriented programming16 boolean expressions and selection statementsObject oriented programming16 boolean expressions and selection statements
Object oriented programming16 boolean expressions and selection statementsVaibhav Khanna
 
Vb decision making statements
Vb decision making statementsVb decision making statements
Vb decision making statementspragya ratan
 

What's hot (20)

Control structure C++
Control structure C++Control structure C++
Control structure C++
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
C# conditional branching statement
C# conditional branching statementC# conditional branching statement
C# conditional branching statement
 
If and select statement
If and select statementIf and select statement
If and select statement
 
Conditional statements
Conditional statementsConditional statements
Conditional statements
 
Control statements in java programmng
Control statements in java programmngControl statements in java programmng
Control statements in java programmng
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Chapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsChapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java Statements
 
Lecture 3 Conditionals, expressions and Variables
Lecture 3   Conditionals, expressions and VariablesLecture 3   Conditionals, expressions and Variables
Lecture 3 Conditionals, expressions and Variables
 
Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in Java
 
Selection statements
Selection statementsSelection statements
Selection statements
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
If-else and switch-case
If-else and switch-caseIf-else and switch-case
If-else and switch-case
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision Control
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C Programming
 
Object oriented programming16 boolean expressions and selection statements
Object oriented programming16 boolean expressions and selection statementsObject oriented programming16 boolean expressions and selection statements
Object oriented programming16 boolean expressions and selection statements
 
Vb decision making statements
Vb decision making statementsVb decision making statements
Vb decision making statements
 

Viewers also liked (20)

Iteration
IterationIteration
Iteration
 
Control Structures in Visual Basic
Control Structures in  Visual BasicControl Structures in  Visual Basic
Control Structures in Visual Basic
 
Control statements
Control statementsControl statements
Control statements
 
Selection Control Structures
Selection Control StructuresSelection Control Structures
Selection Control Structures
 
Business hardware
Business hardwareBusiness hardware
Business hardware
 
Crm
CrmCrm
Crm
 
SE - Software Requirements
SE - Software RequirementsSE - Software Requirements
SE - Software Requirements
 
05 control structures 2
05 control structures 205 control structures 2
05 control structures 2
 
Ch5 - Project Management
Ch5 - Project ManagementCh5 - Project Management
Ch5 - Project Management
 
06 procedures
06 procedures06 procedures
06 procedures
 
SE - System Models
SE - System ModelsSE - System Models
SE - System Models
 
12 gui concepts 1
12 gui concepts 112 gui concepts 1
12 gui concepts 1
 
Jedi course notes intro to programming 1
Jedi course notes intro to programming 1Jedi course notes intro to programming 1
Jedi course notes intro to programming 1
 
Pascal programming language
Pascal programming languagePascal programming language
Pascal programming language
 
Introduction to mobile communication
Introduction to mobile communicationIntroduction to mobile communication
Introduction to mobile communication
 
Control statement in c
Control statement in cControl statement in c
Control statement in c
 
Basic structure of C++ program
Basic structure of C++ programBasic structure of C++ program
Basic structure of C++ program
 
Control structure in c
Control structure in cControl structure in c
Control structure in c
 
Requirements Engineering Process
Requirements Engineering ProcessRequirements Engineering Process
Requirements Engineering Process
 
RPT Sains Komputer Tingkatan 4
RPT Sains Komputer Tingkatan 4RPT Sains Komputer Tingkatan 4
RPT Sains Komputer Tingkatan 4
 

Similar to Control structures selection

CSC111-Chap_03.pdf
CSC111-Chap_03.pdfCSC111-Chap_03.pdf
CSC111-Chap_03.pdf2b75fd3051
 
Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Muhammad Tahir Bashir
 
Ch5 Selection Statements
Ch5 Selection StatementsCh5 Selection Statements
Ch5 Selection StatementsSzeChingChen
 
Control statements anil
Control statements anilControl statements anil
Control statements anilAnil Dutt
 
Week 1: Getting Your Hands Dirty - Part 1
Week 1: Getting Your Hands Dirty - Part 1Week 1: Getting Your Hands Dirty - Part 1
Week 1: Getting Your Hands Dirty - Part 1Jamshid Hashimi
 
0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdf0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdfssusere19c741
 
Intro To C++ - Class 09 - Control Statements: Part 1
Intro To C++ - Class 09 - Control Statements: Part 1Intro To C++ - Class 09 - Control Statements: Part 1
Intro To C++ - Class 09 - Control Statements: Part 1Blue Elephant Consulting
 
Copyright © 2018 Pearson Education, Inc.C H A P T E R 3.docx
Copyright © 2018 Pearson Education, Inc.C H A P T E R  3.docxCopyright © 2018 Pearson Education, Inc.C H A P T E R  3.docx
Copyright © 2018 Pearson Education, Inc.C H A P T E R 3.docxdickonsondorris
 
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 operatorsVaibhav Khanna
 

Similar to Control structures selection (20)

Selection
SelectionSelection
Selection
 
CSC111-Chap_03.pdf
CSC111-Chap_03.pdfCSC111-Chap_03.pdf
CSC111-Chap_03.pdf
 
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.pdf
Ch05.pdfCh05.pdf
Ch05.pdf
 
ch05.ppt
ch05.pptch05.ppt
ch05.ppt
 
Ch5 Selection Statements
Ch5 Selection StatementsCh5 Selection Statements
Ch5 Selection Statements
 
slides03.ppt
slides03.pptslides03.ppt
slides03.ppt
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Week 1: Getting Your Hands Dirty - Part 1
Week 1: Getting Your Hands Dirty - Part 1Week 1: Getting Your Hands Dirty - Part 1
Week 1: Getting Your Hands Dirty - Part 1
 
Python Week 1.pptx
Python Week 1.pptxPython Week 1.pptx
Python Week 1.pptx
 
Lecture-13.ppt
Lecture-13.pptLecture-13.ppt
Lecture-13.ppt
 
0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdf0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdf
 
Control structure
Control structureControl structure
Control structure
 
Ch05-converted.pptx
Ch05-converted.pptxCh05-converted.pptx
Ch05-converted.pptx
 
Intro To C++ - Class 09 - Control Statements: Part 1
Intro To C++ - Class 09 - Control Statements: Part 1Intro To C++ - Class 09 - Control Statements: Part 1
Intro To C++ - Class 09 - Control Statements: Part 1
 
Copyright © 2018 Pearson Education, Inc.C H A P T E R 3.docx
Copyright © 2018 Pearson Education, Inc.C H A P T E R  3.docxCopyright © 2018 Pearson Education, Inc.C H A P T E R  3.docx
Copyright © 2018 Pearson Education, Inc.C H A P T E R 3.docx
 
8 statement level
8 statement level8 statement level
8 statement level
 
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
 
CHAPTER 4- Lesson A
CHAPTER 4- Lesson ACHAPTER 4- Lesson A
CHAPTER 4- Lesson A
 
Lesson 5 .1 selection structure
Lesson 5 .1 selection structureLesson 5 .1 selection structure
Lesson 5 .1 selection structure
 

More from Online

Philosophy of early childhood education 3
Philosophy of early childhood education 3Philosophy of early childhood education 3
Philosophy of early childhood education 3Online
 
Philosophy of early childhood education 2
Philosophy of early childhood education 2Philosophy of early childhood education 2
Philosophy of early childhood education 2Online
 
Philosophy of early childhood education 1
Philosophy of early childhood education 1Philosophy of early childhood education 1
Philosophy of early childhood education 1Online
 
Philosophy of early childhood education 4
Philosophy of early childhood education 4Philosophy of early childhood education 4
Philosophy of early childhood education 4Online
 
Operation and expression in c++
Operation and expression in c++Operation and expression in c++
Operation and expression in c++Online
 
Functions
FunctionsFunctions
FunctionsOnline
 
Formatted input and output
Formatted input and outputFormatted input and output
Formatted input and outputOnline
 
Control structures repetition
Control structures   repetitionControl structures   repetition
Control structures repetitionOnline
 
Introduction to problem solving in c++
Introduction to problem solving in c++Introduction to problem solving in c++
Introduction to problem solving in c++Online
 
Optical transmission technique
Optical transmission techniqueOptical transmission technique
Optical transmission techniqueOnline
 
Multi protocol label switching (mpls)
Multi protocol label switching (mpls)Multi protocol label switching (mpls)
Multi protocol label switching (mpls)Online
 
Lan technologies
Lan technologiesLan technologies
Lan technologiesOnline
 
Introduction to internet technology
Introduction to internet technologyIntroduction to internet technology
Introduction to internet technologyOnline
 
Internet standard routing protocols
Internet standard routing protocolsInternet standard routing protocols
Internet standard routing protocolsOnline
 
Internet protocol
Internet protocolInternet protocol
Internet protocolOnline
 
Application protocols
Application protocolsApplication protocols
Application protocolsOnline
 
Addressing
AddressingAddressing
AddressingOnline
 
Transport protocols
Transport protocolsTransport protocols
Transport protocolsOnline
 
Leadership
LeadershipLeadership
LeadershipOnline
 
Introduction to management
Introduction to managementIntroduction to management
Introduction to managementOnline
 

More from Online (20)

Philosophy of early childhood education 3
Philosophy of early childhood education 3Philosophy of early childhood education 3
Philosophy of early childhood education 3
 
Philosophy of early childhood education 2
Philosophy of early childhood education 2Philosophy of early childhood education 2
Philosophy of early childhood education 2
 
Philosophy of early childhood education 1
Philosophy of early childhood education 1Philosophy of early childhood education 1
Philosophy of early childhood education 1
 
Philosophy of early childhood education 4
Philosophy of early childhood education 4Philosophy of early childhood education 4
Philosophy of early childhood education 4
 
Operation and expression in c++
Operation and expression in c++Operation and expression in c++
Operation and expression in c++
 
Functions
FunctionsFunctions
Functions
 
Formatted input and output
Formatted input and outputFormatted input and output
Formatted input and output
 
Control structures repetition
Control structures   repetitionControl structures   repetition
Control structures repetition
 
Introduction to problem solving in c++
Introduction to problem solving in c++Introduction to problem solving in c++
Introduction to problem solving in c++
 
Optical transmission technique
Optical transmission techniqueOptical transmission technique
Optical transmission technique
 
Multi protocol label switching (mpls)
Multi protocol label switching (mpls)Multi protocol label switching (mpls)
Multi protocol label switching (mpls)
 
Lan technologies
Lan technologiesLan technologies
Lan technologies
 
Introduction to internet technology
Introduction to internet technologyIntroduction to internet technology
Introduction to internet technology
 
Internet standard routing protocols
Internet standard routing protocolsInternet standard routing protocols
Internet standard routing protocols
 
Internet protocol
Internet protocolInternet protocol
Internet protocol
 
Application protocols
Application protocolsApplication protocols
Application protocols
 
Addressing
AddressingAddressing
Addressing
 
Transport protocols
Transport protocolsTransport protocols
Transport protocols
 
Leadership
LeadershipLeadership
Leadership
 
Introduction to management
Introduction to managementIntroduction to management
Introduction to management
 

Recently uploaded

The Singapore Teaching Practice document
The Singapore Teaching Practice documentThe Singapore Teaching Practice document
The Singapore Teaching Practice documentXsasf Sfdfasd
 
Presentation on the Basics of Writing. Writing a Paragraph
Presentation on the Basics of Writing. Writing a ParagraphPresentation on the Basics of Writing. Writing a Paragraph
Presentation on the Basics of Writing. Writing a ParagraphNetziValdelomar1
 
3.19.24 Urban Uprisings and the Chicago Freedom Movement.pptx
3.19.24 Urban Uprisings and the Chicago Freedom Movement.pptx3.19.24 Urban Uprisings and the Chicago Freedom Movement.pptx
3.19.24 Urban Uprisings and the Chicago Freedom Movement.pptxmary850239
 
Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.EnglishCEIPdeSigeiro
 
NOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdf
NOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdfNOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdf
NOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdfSumit Tiwari
 
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRATanmoy Mishra
 
Ultra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxUltra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxDr. Asif Anas
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational PhilosophyShuvankar Madhu
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17Celine George
 
How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17Celine George
 
How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17Celine George
 
How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17Celine George
 
UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE
 
Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.raviapr7
 
How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17Celine George
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxraviapr7
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?TechSoup
 
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptxClinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptxraviapr7
 
Human-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesHuman-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesMohammad Hassany
 

Recently uploaded (20)

The Singapore Teaching Practice document
The Singapore Teaching Practice documentThe Singapore Teaching Practice document
The Singapore Teaching Practice document
 
Presentation on the Basics of Writing. Writing a Paragraph
Presentation on the Basics of Writing. Writing a ParagraphPresentation on the Basics of Writing. Writing a Paragraph
Presentation on the Basics of Writing. Writing a Paragraph
 
3.19.24 Urban Uprisings and the Chicago Freedom Movement.pptx
3.19.24 Urban Uprisings and the Chicago Freedom Movement.pptx3.19.24 Urban Uprisings and the Chicago Freedom Movement.pptx
3.19.24 Urban Uprisings and the Chicago Freedom Movement.pptx
 
Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.
 
NOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdf
NOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdfNOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdf
NOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdf
 
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
 
Ultra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxUltra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptx
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational Philosophy
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17
 
How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17
 
How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17
 
Finals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quizFinals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quiz
 
How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17
 
UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024
 
Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.
 
How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptx
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?
 
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptxClinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
 
Human-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesHuman-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming Classes
 

Control structures selection

  • 2. Objectives In this chapter, you will: • Learn about control structures • Examine relational and logical operators • Explore how to form and evaluate logical (Boolean) expressions • Discover how to use the selection control structures if, if...else, and switch in a program 2
  • 3. Control Structures • A computer can proceed: – In sequence – Selectively (branch) - making a choice – Repetitively (iteratively) - looping • Some statements are executed only if certain conditions are met • A condition is met if it evaluates to true 3
  • 5. Relational Operators • A condition is represented by a logical (Boolean) expression that can be true or false • Relational operators: – Allow comparisons – Require two operands (binary) – Evaluate to true or false 5
  • 7. Relational Operators (cont.) • You can use the relational operators with all three simple data types: – 8 < 15 evaluates to true – 6 != 6 evaluates to false – 2.5 > 5.8 evaluates to false – 5.9 <= 7.5 evaluates to true 7
  • 9. Logical (Boolean) Operators and Logical Expressions • Logical (Boolean) operators enable you to combine logical expressions unary binary binary 9
  • 10. Logical (Boolean) Operators and Logical Expressions (cont.) 10
  • 11. 11
  • 12. 12
  • 13. Order of Precedence • Relational and logical operators are evaluated from left to right • The associativity is left to right • Parentheses can override precedence 13
  • 14. Order of Precedence (cont.) 14
  • 15. Order of Precedence (cont.) 15
  • 16. Order of Precedence (cont.) 16
  • 17. Order of Precedence (cont.) 17
  • 18. 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 18
  • 19. Selection: if and if...else (cont.) • 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 19
  • 21. 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 21
  • 24. Compound (Block of) Statement • Compound statement (block of statements): • A compound statement is a single statement 24
  • 25. Compound (Block of) Statement (cont.) 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; } 25
  • 26. 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 26
  • 27. 27
  • 28. Comparing if…else Statements with a Series of if Statements 28
  • 29. 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 29
  • 30. switch Structures • switch structure: alternate to if-else • switch (integral) expression is evaluated first • Value of the expression determines which corresponding action is taken • Expression is sometimes called the selector 30
  • 31. 31
  • 32. switch Structures (cont.) • One or more statements may follow a case label • Braces are not needed to turn multiple statements into a single compound statement • The break statement may or may not appear after each statement • switch, case, break, and default are reserved words 32
  • 33. 33
  • 34. Summary • Control structures alter normal control flow • Most common control structures are selection and repetition • Relational operators: ==, <, <=, >, >=, != • Logical expressions evaluate to 1 (true) or 0 (false) • Logical operators: ! (not), && (and), || (or) • Two selection structures: one-way selection and two-way selection • The expression in an if or if...else structure is usually a logical expression 34
  • 35. Summary (continued) • No stand-alone else statement in C++ – Every else has a related if • A sequence of statements enclosed between braces, { and }, is called a compound statement or block of statements • Using assignment in place of the equality operator creates a semantic error • switch structure handles multiway selection • break statement ends switch statement Source: C++ Programming: From Problem Analysis to Program Design, Fourth Edition 35