SlideShare ist ein Scribd-Unternehmen logo
1 von 25
Downloaden Sie, um offline zu lesen
Introduction to Programming in C++
Eight Edition
Lesson 5.1:
The Selection Structure
An Introduction to Programming with C++, Eight Edition
• Include the selection structure in pseudocode and in a
flowchart
• Code a selection structure using the if statement
• Include comparison operators in a selection structure’s
condition
• Include logical operators in a selection structure’s
condition
• Temporarily convert a character to either uppercase or
lowercase
• Format numeric output
Objectives
2
An Introduction to Programming with C++, Eight Edition
• With only the sequence structure, all instructions are
executed in order
• A selection structure is needed when a decision must be
made (based on some condition) before selecting an
instruction to execute
• A selection structure’s condition must be phrased as a
Boolean expression (evaluates to true or false)
Making Decisions
3
An Introduction to Programming with C++, Eight Edition
• Single-alternative selection structure – Set of instructions is
executed only if condition evaluates to true
• Dual-alternative selection structure
– Executes different sets of instructions based on
whether condition evaluates to true or false
• True path
– Instructions followed when condition evaluates
to true
• False path
– Instructions followed when condition evaluates to
false
Making Decisions (cont’d.)
4
An Introduction to Programming with C++, Eight Edition
Making Decisions (cont’d.)
5
An Introduction to Programming with C++, Eight Edition
Making Decisions (cont’d.)
6
An Introduction to Programming with C++, Eight Edition
Making Decisions (cont’d.)
7
An Introduction to Programming with C++, Eight Edition
• Recall from Chapter 2:
– Oval = start/stop symbol; rectangle = process symbol;
parallelogram = input/output symbol
• Diamond symbol is called decision symbol
– Used to represent condition (decision) in selection and
repetition structures
• Selection structures have one flowline leading in and
two leading out
– “T” line leading out points to true path
– “F” line leading out points to false path
Flowcharting a Selection Structure
8
An Introduction to Programming with C++, Eight Edition
Flowcharting a Selection Structure
9
An Introduction to Programming with C++, Eight Edition
• The if (and else) statement is used to code most
selection structures in C++
• Syntax
if (condition)
one or more statements (true path)
[else
one or more statements (false path)]
• Keyword if and condition are required
• Portion in brackets (else clause) is optional
– Only used for dual-alternative selection structures
Coding a Selection Structure in C++
10
An Introduction to Programming with C++, Eight Edition
• Programmer must supply condition and statements in true
(and, optionally, false) path
• If path contains more than one statement, must be entered
as a statement block (enclosed in {})
• Good programming practice to put comment at end of
clause (example: //end if)
– Makes program easier to read and understand
• The condition must be a Boolean expression
– May contain variables, constants, arithmetic
operators, comparison operators, and logical operators
Coding a Selection Structure in C++
(cont’d.)
11
An Introduction to Programming with C++, Eight Edition
Coding a Selection Structure in C++
(cont’d.)
12
An Introduction to Programming with C++, Eight Edition
Coding a Selection Structure in C++
(cont’d.)
13
An Introduction to Programming with C++, Eight Edition
• Comparison operators are used to compare two values
that have the same data type
– less than (<=)
– greater than (>)
– greater than or equal to (>=)
– equal to (==)
– not equal to (!=)
• No spaces between dual-character symbols
Comparison Operators
14
An Introduction to Programming with C++, Eight Edition
• Have precedence numbers like arithmetic operators
• <=, >, and >= have precedence value 1
• == and != have precedence value 2
• Lower precedence evaluated first
• Equal precedence evaluated left to right
• Parentheses used to override precedence order
Comparison Operators (cont’d.)
15
An Introduction to Programming with C++, Eight Edition
• Expressions with comparison operators always evaluate
to Boolean values
• Don’t confuse equality operator (==) with assignment
operator (=)
• Don’t use equality (==) or inequality operator (!=) to
compare real numbers
– Real numbers cannot be stored exactly
– Instead, test that absolute value of their difference is
within some small threshold
Comparison Operators (cont’d.)
16
An Introduction to Programming with C++, Eight Edition
Comparison Operators (cont’d.)
17
An Introduction to Programming with C++, Eight Edition
Comparison Operators (cont’d.)
18
An Introduction to Programming with C++, Eight Edition
• Example program (next slide) takes in two integers,
swaps them if first is greater, and then prints out lower
number, followed by higher number
• Uses single-alternative selection structure with
statement block in true path
• Creates temporary variable to accomplish swap
• Temp variable can only be used in statement block in
which it is declared; called a local variable
Swapping Numeric Values
19
An Introduction to Programming with C++, Eight Edition
• PROGRAM
Swapping Numeric Values (cont’d.)
20
#include<iostream>
Using namespace std;
Int main()
{
int num1 = 0,num2=0;
cout<<”Enter num1:”;
cin>>num1;
cout<<”Enter num2”;
cin>>num2;
if(num1 > num2)
{
int temp =0;
temp = num1;
num1 = num2;
num2 = temp;
} end if
cout<<”Lowest”<<num1<<endl;
cout<<”Highest”<<num2<<endl;
Output:
Enter num1: 99
Enter num2 :84
Lowest : 84
Highest : 99
An Introduction to Programming with C++, Eight Edition
Swapping Numeric Values (cont’d.)
21
An Introduction to Programming with C++, Eight Edition
Swapping Numeric Values (cont’d.)
22
An Introduction to Programming with C++, Eight Edition
Displaying the Area or Circumference
23
#include<iostream>
using namespace std;
const double PI = 3.14;
double radius = 0.0;
int choice = 0;
double answer = 0.0;
cout<<“Enter the radius:”;
cin>>radius;
cout<<“Enter 1 (area) or 2 (Circumference):”;
cin>>choice;
if (choice = =1)
{
answer = radius * radius * PI;
cout<<“Area: “ << answer << endl;
}
else
{
answer = 2*radius*PI;
cout<<“Conference”<< answer << endl;
}
An Introduction to Programming with C++, Eight Edition
Displaying the Area or Circumference
24
An Introduction to Programming with C++, Eight Edition
The End ♥ ♥ ♥
25

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14
 
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14
 
Data flow model -Lecture-4
Data flow model -Lecture-4Data flow model -Lecture-4
Data flow model -Lecture-4
 
Programming in c (importance of c)
Programming in c (importance of c)Programming in c (importance of c)
Programming in c (importance of c)
 
Verilog operators
Verilog operatorsVerilog operators
Verilog operators
 
test(3)arithmetic in c
test(3)arithmetic in ctest(3)arithmetic in c
test(3)arithmetic in c
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Code Generation
Code GenerationCode Generation
Code Generation
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12
 
Unit I - Evaluation of expression
Unit I - Evaluation of expressionUnit I - Evaluation of expression
Unit I - Evaluation of expression
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
 
C basics
C basicsC basics
C basics
 
Assignment9
Assignment9Assignment9
Assignment9
 
Functions
FunctionsFunctions
Functions
 
Ppl
PplPpl
Ppl
 
9781285852744 ppt ch08
9781285852744 ppt ch089781285852744 ppt ch08
9781285852744 ppt ch08
 
Lesson 10
Lesson 10Lesson 10
Lesson 10
 
Compiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent OptimizationsCompiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent Optimizations
 

Ähnlich wie Lesson 5 .1 selection structure

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
 
Review chapter 1 2-3
Review chapter 1 2-3Review chapter 1 2-3
Review chapter 1 2-3ahmed22dg
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming LanguageAhmad Idrees
 
Acm aleppo cpc training introduction 1
Acm aleppo cpc training introduction 1Acm aleppo cpc training introduction 1
Acm aleppo cpc training introduction 1Ahmad Bashar Eter
 
Lecture 2 variables
Lecture 2 variablesLecture 2 variables
Lecture 2 variablesTony Apreku
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02Terry Yoast
 
PPT slide_chapter 02 Basic element of C++.ppt
PPT slide_chapter 02 Basic element of C++.pptPPT slide_chapter 02 Basic element of C++.ppt
PPT slide_chapter 02 Basic element of C++.pptmasadjie
 
UoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdfUoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdfmadihamaqbool6
 
07 control+structures
07 control+structures07 control+structures
07 control+structuresbaran19901990
 
C++ unit-1-part-11
C++ unit-1-part-11C++ unit-1-part-11
C++ unit-1-part-11Jadavsejal
 
chapter11 - Programming.pdf
chapter11 - Programming.pdfchapter11 - Programming.pdf
chapter11 - Programming.pdfsatonaka3
 
Computer Programming
Computer ProgrammingComputer Programming
Computer ProgrammingBurhan Fakhar
 
Chap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxChap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxRonaldo Aditya
 

Ähnlich wie Lesson 5 .1 selection structure (20)

C++ ch2
C++ ch2C++ ch2
C++ ch2
 
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
 
Review chapter 1 2-3
Review chapter 1 2-3Review chapter 1 2-3
Review chapter 1 2-3
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 
Acm aleppo cpc training introduction 1
Acm aleppo cpc training introduction 1Acm aleppo cpc training introduction 1
Acm aleppo cpc training introduction 1
 
C_plus_plus
C_plus_plusC_plus_plus
C_plus_plus
 
Lecture 2 variables
Lecture 2 variablesLecture 2 variables
Lecture 2 variables
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
 
PPT slide_chapter 02 Basic element of C++.ppt
PPT slide_chapter 02 Basic element of C++.pptPPT slide_chapter 02 Basic element of C++.ppt
PPT slide_chapter 02 Basic element of C++.ppt
 
C++.ppt
C++.pptC++.ppt
C++.ppt
 
Chapter 4 5
Chapter 4 5Chapter 4 5
Chapter 4 5
 
UoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdfUoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdf
 
07 control+structures
07 control+structures07 control+structures
07 control+structures
 
C++ unit-1-part-11
C++ unit-1-part-11C++ unit-1-part-11
C++ unit-1-part-11
 
chapter11 - Programming.pdf
chapter11 - Programming.pdfchapter11 - Programming.pdf
chapter11 - Programming.pdf
 
ch5.ppt
ch5.pptch5.ppt
ch5.ppt
 
Computer Programming
Computer ProgrammingComputer Programming
Computer Programming
 
Intro to c++
Intro to c++Intro to c++
Intro to c++
 
Chap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxChap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptx
 
#Code2 create c++ for beginners
#Code2 create  c++ for beginners #Code2 create  c++ for beginners
#Code2 create c++ for beginners
 

Mehr von MLG College of Learning, Inc (20)

PC111.Lesson2
PC111.Lesson2PC111.Lesson2
PC111.Lesson2
 
PC111.Lesson1
PC111.Lesson1PC111.Lesson1
PC111.Lesson1
 
PC111-lesson1.pptx
PC111-lesson1.pptxPC111-lesson1.pptx
PC111-lesson1.pptx
 
PC LEESOON 6.pptx
PC LEESOON 6.pptxPC LEESOON 6.pptx
PC LEESOON 6.pptx
 
PC 106 PPT-09.pptx
PC 106 PPT-09.pptxPC 106 PPT-09.pptx
PC 106 PPT-09.pptx
 
PC 106 PPT-07
PC 106 PPT-07PC 106 PPT-07
PC 106 PPT-07
 
PC 106 PPT-01
PC 106 PPT-01PC 106 PPT-01
PC 106 PPT-01
 
PC 106 PPT-06
PC 106 PPT-06PC 106 PPT-06
PC 106 PPT-06
 
PC 106 PPT-05
PC 106 PPT-05PC 106 PPT-05
PC 106 PPT-05
 
PC 106 Slide 04
PC 106 Slide 04PC 106 Slide 04
PC 106 Slide 04
 
PC 106 Slide no.02
PC 106 Slide no.02PC 106 Slide no.02
PC 106 Slide no.02
 
pc-106-slide-3
pc-106-slide-3pc-106-slide-3
pc-106-slide-3
 
PC 106 Slide 2
PC 106 Slide 2PC 106 Slide 2
PC 106 Slide 2
 
PC 106 Slide 1.pptx
PC 106 Slide 1.pptxPC 106 Slide 1.pptx
PC 106 Slide 1.pptx
 
Db2 characteristics of db ms
Db2 characteristics of db msDb2 characteristics of db ms
Db2 characteristics of db ms
 
Db1 introduction
Db1 introductionDb1 introduction
Db1 introduction
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 
Lesson 3.1
Lesson 3.1Lesson 3.1
Lesson 3.1
 
Lesson 1.6
Lesson 1.6Lesson 1.6
Lesson 1.6
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 

Kürzlich hochgeladen

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 

Kürzlich hochgeladen (20)

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

Lesson 5 .1 selection structure

  • 1. Introduction to Programming in C++ Eight Edition Lesson 5.1: The Selection Structure
  • 2. An Introduction to Programming with C++, Eight Edition • Include the selection structure in pseudocode and in a flowchart • Code a selection structure using the if statement • Include comparison operators in a selection structure’s condition • Include logical operators in a selection structure’s condition • Temporarily convert a character to either uppercase or lowercase • Format numeric output Objectives 2
  • 3. An Introduction to Programming with C++, Eight Edition • With only the sequence structure, all instructions are executed in order • A selection structure is needed when a decision must be made (based on some condition) before selecting an instruction to execute • A selection structure’s condition must be phrased as a Boolean expression (evaluates to true or false) Making Decisions 3
  • 4. An Introduction to Programming with C++, Eight Edition • Single-alternative selection structure – Set of instructions is executed only if condition evaluates to true • Dual-alternative selection structure – Executes different sets of instructions based on whether condition evaluates to true or false • True path – Instructions followed when condition evaluates to true • False path – Instructions followed when condition evaluates to false Making Decisions (cont’d.) 4
  • 5. An Introduction to Programming with C++, Eight Edition Making Decisions (cont’d.) 5
  • 6. An Introduction to Programming with C++, Eight Edition Making Decisions (cont’d.) 6
  • 7. An Introduction to Programming with C++, Eight Edition Making Decisions (cont’d.) 7
  • 8. An Introduction to Programming with C++, Eight Edition • Recall from Chapter 2: – Oval = start/stop symbol; rectangle = process symbol; parallelogram = input/output symbol • Diamond symbol is called decision symbol – Used to represent condition (decision) in selection and repetition structures • Selection structures have one flowline leading in and two leading out – “T” line leading out points to true path – “F” line leading out points to false path Flowcharting a Selection Structure 8
  • 9. An Introduction to Programming with C++, Eight Edition Flowcharting a Selection Structure 9
  • 10. An Introduction to Programming with C++, Eight Edition • The if (and else) statement is used to code most selection structures in C++ • Syntax if (condition) one or more statements (true path) [else one or more statements (false path)] • Keyword if and condition are required • Portion in brackets (else clause) is optional – Only used for dual-alternative selection structures Coding a Selection Structure in C++ 10
  • 11. An Introduction to Programming with C++, Eight Edition • Programmer must supply condition and statements in true (and, optionally, false) path • If path contains more than one statement, must be entered as a statement block (enclosed in {}) • Good programming practice to put comment at end of clause (example: //end if) – Makes program easier to read and understand • The condition must be a Boolean expression – May contain variables, constants, arithmetic operators, comparison operators, and logical operators Coding a Selection Structure in C++ (cont’d.) 11
  • 12. An Introduction to Programming with C++, Eight Edition Coding a Selection Structure in C++ (cont’d.) 12
  • 13. An Introduction to Programming with C++, Eight Edition Coding a Selection Structure in C++ (cont’d.) 13
  • 14. An Introduction to Programming with C++, Eight Edition • Comparison operators are used to compare two values that have the same data type – less than (<=) – greater than (>) – greater than or equal to (>=) – equal to (==) – not equal to (!=) • No spaces between dual-character symbols Comparison Operators 14
  • 15. An Introduction to Programming with C++, Eight Edition • Have precedence numbers like arithmetic operators • <=, >, and >= have precedence value 1 • == and != have precedence value 2 • Lower precedence evaluated first • Equal precedence evaluated left to right • Parentheses used to override precedence order Comparison Operators (cont’d.) 15
  • 16. An Introduction to Programming with C++, Eight Edition • Expressions with comparison operators always evaluate to Boolean values • Don’t confuse equality operator (==) with assignment operator (=) • Don’t use equality (==) or inequality operator (!=) to compare real numbers – Real numbers cannot be stored exactly – Instead, test that absolute value of their difference is within some small threshold Comparison Operators (cont’d.) 16
  • 17. An Introduction to Programming with C++, Eight Edition Comparison Operators (cont’d.) 17
  • 18. An Introduction to Programming with C++, Eight Edition Comparison Operators (cont’d.) 18
  • 19. An Introduction to Programming with C++, Eight Edition • Example program (next slide) takes in two integers, swaps them if first is greater, and then prints out lower number, followed by higher number • Uses single-alternative selection structure with statement block in true path • Creates temporary variable to accomplish swap • Temp variable can only be used in statement block in which it is declared; called a local variable Swapping Numeric Values 19
  • 20. An Introduction to Programming with C++, Eight Edition • PROGRAM Swapping Numeric Values (cont’d.) 20 #include<iostream> Using namespace std; Int main() { int num1 = 0,num2=0; cout<<”Enter num1:”; cin>>num1; cout<<”Enter num2”; cin>>num2; if(num1 > num2) { int temp =0; temp = num1; num1 = num2; num2 = temp; } end if cout<<”Lowest”<<num1<<endl; cout<<”Highest”<<num2<<endl; Output: Enter num1: 99 Enter num2 :84 Lowest : 84 Highest : 99
  • 21. An Introduction to Programming with C++, Eight Edition Swapping Numeric Values (cont’d.) 21
  • 22. An Introduction to Programming with C++, Eight Edition Swapping Numeric Values (cont’d.) 22
  • 23. An Introduction to Programming with C++, Eight Edition Displaying the Area or Circumference 23 #include<iostream> using namespace std; const double PI = 3.14; double radius = 0.0; int choice = 0; double answer = 0.0; cout<<“Enter the radius:”; cin>>radius; cout<<“Enter 1 (area) or 2 (Circumference):”; cin>>choice; if (choice = =1) { answer = radius * radius * PI; cout<<“Area: “ << answer << endl; } else { answer = 2*radius*PI; cout<<“Conference”<< answer << endl; }
  • 24. An Introduction to Programming with C++, Eight Edition Displaying the Area or Circumference 24
  • 25. An Introduction to Programming with C++, Eight Edition The End ♥ ♥ ♥ 25