SlideShare a Scribd company logo
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

More Related Content

What's hot

Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14
IIUM
 
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14
IIUM
 
Data flow model -Lecture-4
Data flow model -Lecture-4Data flow model -Lecture-4
Data flow model -Lecture-4
Dr.YNM
 
Programming in c (importance of c)
Programming in c (importance of c)Programming in c (importance of c)
Programming in c (importance of c)
ViswanathanS21
 
Verilog operators
Verilog operatorsVerilog operators
Verilog operators
Dr.YNM
 
test(3)arithmetic in c
test(3)arithmetic in ctest(3)arithmetic in c
test(3)arithmetic in c
Jaya Malathy
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
Tanveer Malik
 
Code Generation
Code GenerationCode Generation
Code Generation
PrabuPappuR
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
programming9
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
Akhil Kaushik
 
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12
Terry Yoast
 
Unit I - Evaluation of expression
Unit I - Evaluation of expressionUnit I - Evaluation of expression
Unit I - Evaluation of expression
DrkhanchanaR
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
Siva Sathya
 
C basics
C basicsC basics
C basics
sridevi5983
 
Assignment9
Assignment9Assignment9
Assignment9
Sunita Milind Dol
 
Functions
FunctionsFunctions
Ppl
PplPpl
9781285852744 ppt ch08
9781285852744 ppt ch089781285852744 ppt ch08
9781285852744 ppt ch08
Terry Yoast
 
Lesson 10
Lesson 10Lesson 10
Compiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent OptimizationsCompiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent Optimizations
Jyothishmathi Institute of Technology and Science Karimnagar
 

What's hot (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
 

Similar to Lesson 5 .1 selection structure

C++ ch2
C++ ch2C++ 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
ssusere19c741
 
Review chapter 1 2-3
Review chapter 1 2-3Review chapter 1 2-3
Review chapter 1 2-3
ahmed22dg
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
Ahmad Idrees
 
Acm aleppo cpc training introduction 1
Acm aleppo cpc training introduction 1Acm aleppo cpc training introduction 1
Acm aleppo cpc training introduction 1
Ahmad Bashar Eter
 
C_plus_plus
C_plus_plusC_plus_plus
C_plus_plus
Ralph Weber
 
Lecture 2 variables
Lecture 2 variablesLecture 2 variables
Lecture 2 variables
Tony Apreku
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
Terry 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++.ppt
masadjie
 
C++.ppt
C++.pptC++.ppt
C++.ppt
shweta210
 
Chapter 4 5
Chapter 4 5Chapter 4 5
Chapter 4 5
ahmed22dg
 
UoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdfUoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdf
madihamaqbool6
 
07 control+structures
07 control+structures07 control+structures
07 control+structures
baran19901990
 
chapter11 - Programming.pdf
chapter11 - Programming.pdfchapter11 - Programming.pdf
chapter11 - Programming.pdf
satonaka3
 
ch5.ppt
ch5.pptch5.ppt
ch5.ppt
ssusere004a8
 
Computer Programming
Computer ProgrammingComputer Programming
Computer Programming
Burhan Fakhar
 
Intro to c++
Intro to c++Intro to c++
Intro to c++
temkin abdlkader
 
Chap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxChap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptx
Ronaldo Aditya
 
#Code2 create c++ for beginners
#Code2 create  c++ for beginners #Code2 create  c++ for beginners
#Code2 create c++ for beginners
GDGKuwaitGoogleDevel
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Mohammed Sikander
 

Similar to 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
 
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
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 

More from MLG College of Learning, Inc

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

More from 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
 

Recently uploaded

Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
Nguyen Thanh Tu Collection
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
zuzanka
 
How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17
Celine George
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
Steve Thomason
 
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGHKHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
shreyassri1208
 
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptxCapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapitolTechU
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
RidwanHassanYusuf
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
nitinpv4ai
 
HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.
deepaannamalai16
 
MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025
khuleseema60
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Henry Hollis
 
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxxSimple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
RandolphRadicy
 
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdfREASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
giancarloi8888
 
Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.
IsmaelVazquez38
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17
Celine George
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
zuzanka
 
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
Kalna College
 

Recently uploaded (20)

Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
 
How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
 
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGHKHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
 
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptxCapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
 
HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.
 
MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
 
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxxSimple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
 
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdfREASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
 
Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
 
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
 

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