際際滷

際際滷Share a Scribd company logo
Programming
Fundamentals
By
Adnan Waheed
University of Narowal
Control Structure
(Selection Statements)
Lecture No. 12
Tuesday, November 21, 2023
Avoiding Bugs by Avoiding Partially
Understood Concepts and
Techniques
 Must use concepts and techniques
correctly;
 Otherwise solution will be either incorrect or
deficient
 If you do not understand a concept or
technique completely
 Dont use it
 Save yourself an enormous amount of
debugging time
C++ Programming: Program Design Including Data Structures, Fifth Edition 2
Input Failure and the if
Statement
 If input stream enters a fail state
 All subsequent input statements associated
with that stream are ignored
 Program continues to execute
 May produce erroneous results
 Can use if statements to check status of
input stream
 If stream enters the fail state, include
instructions that stop program execution
C++ Programming: Program Design Including Data Structures, Fifth Edition 3
Confusion Between the Equality
(==) and Assignment (=) Operators
 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
C++ Programming: Program Design Including Data Structures, Fifth Edition 4
Program Style and Form
(Revisited): Indentation
 If your program is properly indented
 Spot and fix errors quickly
 Show the natural grouping of statements
 Insert a blank line between statements that
are naturally separate
 Two commonly used styles for placing braces
 On a line by themselves
 Or left brace is placed after the expression, and
the right brace is on a line by itself
C++ Programming: Program Design Including Data Structures, Fifth Edition 5
Using Pseudocode to Develop,
Test, and Debug a Program
 Pseudocode, or just pseudo
 Informal mixture of C++ and ordinary
language
 Helps you quickly develop the correct
structure of the program and avoid making
common errors
 Use a wide range of values in a walk-
through to evaluate the program
C++ Programming: Program Design Including Data Structures, Fifth Edition 6
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
C++ Programming: Program Design Including Data Structures, Fifth Edition 7
switch Structures
 Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
C++ Programming: Program Design Including Data Structures, Fifth Edition 8
The switch expression is
evaluated once
The value of the expression is
compared with the values of
each case
If there is a match, the
associated block of code is
executed
The break and default keywords
are optional
switch Structures (cont'd.)
C++ Programming: Program Design Including Data Structures, Fifth Edition 9
Switch Example
 The example below uses the weekday number to calculate the weekday
name:
int day = 4;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break; // Outputs "Thursday" (day 4)
C++ Programming: Program Design Including Data Structures, Fifth Edition 10
The break Keyword
C++ Programming: Program Design Including Data Structures, Fifth Edition 11
 When C++ reaches a break keyword, it breaks out of the switch
block.
 This will stop the execution of more code and case testing inside
the block.
 When a match is found, and the job is done, it's time for a break.
There is no need for more testing.
The default Keyword
C++ Programming: Program Design Including Data Structures, Fifth Edition 12
The default keyword specifies some code to run if there is no case match:
int day = 4;
switch (day) {
case 6:
cout << "Today is Saturday";
break;
case 7:
cout << "Today is Sunday";
break;
default:
cout << "Looking forward to the Weekend";
}
// Outputs "Looking forward to the Weekend.
switch Structures (cont'd.)
C++ Programming: Program Design Including Data Structures, Fifth Edition 13
C++ Programming: Program Design Including Data Structures, Fifth Edition 14
Output = ?
C++ Programming: Program Design Including Data Structures, Fifth Edition 15
Avoiding Bugs by Avoiding Partially
Understood Concepts and
Techniques: Revisited
 To output results correctly
 The switch structure must include a break
statement after each cout statement
C++ Programming: Program Design Including Data Structures, Fifth Edition 16
COMPLETE EXAMPLE @
HOME FOR PRACTICE OF
STUDENTS
C++ Programming: Program Design Including Data Structures, Fifth Edition 17
Programming Example: Cable
Company Billing
 This programming example calculates a
customers bill for a local cable company
 There are two types of customers:
 Residential
 Business
 Two rates for calculating a cable bill:
 One for residential customers
 One for business customers
C++ Programming: Program Design Including Data Structures, Fifth Edition 18
Programming Example: Rates
 For residential customer:
 Bill processing fee: $4.50
 Basic service fee: $20.50
 Premium channel: $7.50 per channel
 For business customer:
 Bill processing fee: $15.00
 Basic service fee: $75.00 for first 10
connections/$5.00 for each additional one
 Premium channel cost: $50.00 per channel for
any number of connections
C++ Programming: Program Design Including Data Structures, Fifth Edition 19
Programming Example:
Requirements
 Ask user for account number and
customer code
 Assume R or r stands for residential
customer and B or b stands for business
customer
C++ Programming: Program Design Including Data Structures, Fifth Edition 20
Programming Example: Input and
Output
 Input:
 Customer account number
 Customer code
 Number of premium channels
 For business customers, number of basic
service connections
 Output:
 Customers account number
 Billing amount
C++ Programming: Program Design Including Data Structures, Fifth Edition 21
Programming Example: Program
Analysis
 Purpose: calculate and print billing amount
 Calculating billing amount requires:
 Customer for whom the billing amount is
calculated (residential or business)
 Number of premium channels to which the
customer subscribes
 For a business customer, you need:
 Number of basic service connections
 Number of premium channels
C++ Programming: Program Design Including Data Structures, Fifth Edition 22
Programming Example: Program
Analysis (cont'd.)
 Data needed to calculate the bill, such as
bill processing fees and the cost of a
premium channel, are known quantities
 The program should print the billing
amount to two decimal places
C++ Programming: Program Design Including Data Structures, Fifth Edition 23
Programming Example: Algorithm
Design
 Set precision to two decimal places
 Prompt user for account number and
customer type
 If customer type is R or r
 Prompt user for number of premium channels
 Compute and print the bill
 If customer type is B or b
 Prompt user for number of basic service
connections and number of premium channels
 Compute and print the bill
C++ Programming: Program Design Including Data Structures, Fifth Edition 24
Programming Example: Variables
and Named Constants
C++ Programming: Program Design Including Data Structures, Fifth Edition 25
Programming Example:
Formulas
Billing for residential customers:
amountDue = RES_BILL_PROC_FEES +
RES_BASIC_SERV_COST
+ numOfPremChannels *
RES_COST_PREM_CHANNEL;
C++ Programming: Program Design Including Data Structures, Fifth Edition 26
Programming Example: Formulas
(cont'd.)
Billing for business customers:
if (numOfBasicServConn <= 10)
amountDue = BUS_BILL_PROC_FEES +
BUS_BASIC_SERV_COST
+ numOfPremChannels *
BUS_COST_PREM_CHANNEL;
else
amountDue = BUS_BILL_PROC_FEES +
BUS_BASIC_SERV_COST
+ (numOfBasicServConn - 10)
* BUS_BASIC_CONN_COST
+ numOfPremChannels *
BUS_COST_PREM_CHANNEL;
C++ Programming: Program Design Including Data Structures, Fifth Edition 27
Programming Example: Main
Algorithm
1. Output floating-point numbers in fixed
decimal with decimal point and trailing zeros
 Output floating-point numbers with two decimal
places and set the precision to two decimal
places
2. Prompt user to enter account number
3. Get customer account number
4. Prompt user to enter customer code
5. Get customer code
C++ Programming: Program Design Including Data Structures, Fifth Edition 28
Programming Example: Main
Algorithm (cont'd.)
6. If the customer code is r or R,
 Prompt user to enter number of premium
channels
 Get the number of premium channels
 Calculate the billing amount
 Print account number and billing amount
C++ Programming: Program Design Including Data Structures, Fifth Edition 29
Programming Example: Main
Algorithm (cont'd.)
7. If customer code is b or B,
 Prompt user to enter number of basic
service connections
 Get number of basic service connections
 Prompt user to enter number of premium
channels
 Get number of premium channels
 Calculate billing amount
 Print account number and billing amount
C++ Programming: Program Design Including Data Structures, Fifth Edition 30
Programming Example: Main
Algorithm (cont'd.)
8. If customer code is other than r, R, b,
or B, output an error message
C++ Programming: Program Design Including Data Structures, Fifth Edition 31
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)
C++ Programming: Program Design Including Data Structures, Fifth Edition 32
Summary (cont'd.)
 Two selection structures: one-way selection
and two-way selection
 The expression in an if or if...else
structure is usually a logical expression
 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
C++ Programming: Program Design Including Data Structures, Fifth Edition 33
Summary (cont'd.)
 Using assignment in place of the equality
operator creates a semantic error
 switch structure handles multiway
selection
 break statement ends switch statement
 Use assert to terminate a program if
certain conditions are not met
C++ Programming: Program Design Including Data Structures, Fifth Edition 34

More Related Content

Similar to UoN-Lec_12_Control_Structure.pdf (20)

Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab new
eyavagal
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++
Mohamed El Desouki
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab new
Last7693
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab new
scottbrownnn
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptxKMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
JessylyneSophia
c++ referesher 1.pdf
c++ referesher 1.pdfc++ referesher 1.pdf
c++ referesher 1.pdf
AnkurSingh656748
Computer programming all chapters
Computer programming all chaptersComputer programming all chapters
Computer programming all chapters
Ibrahim Elewah
ch01_an overview of computers and programming languages
ch01_an overview of computers and programming languagesch01_an overview of computers and programming languages
ch01_an overview of computers and programming languages
LiemLe21
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12
Terry Yoast
Fundamental of Programming Language UNIT-I
Fundamental of Programming Language UNIT-IFundamental of Programming Language UNIT-I
Fundamental of Programming Language UNIT-I
TruptiWable1
U19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptU19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).ppt
Manivannan837728
Debugging and Profiling C++ Template Metaprograms
Debugging and Profiling C++ Template MetaprogramsDebugging and Profiling C++ Template Metaprograms
Debugging and Profiling C++ Template Metaprograms
Platonov Sergey
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
C++ ch1
C++ ch1C++ ch1
C++ ch1
Venkateswarlu Vuggam
Object oriented programming 12 programming steps in cpp and example
Object oriented programming 12 programming steps in cpp and exampleObject oriented programming 12 programming steps in cpp and example
Object oriented programming 12 programming steps in cpp and example
Vaibhav Khanna
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++
CHANDERPRABHU JAIN COLLEGE OF HIGHER STUDIES & SCHOOL OF LAW
COMP 122 Entire Course NEW
COMP 122 Entire Course NEWCOMP 122 Entire Course NEW
COMP 122 Entire Course NEW
shyamuopeight
Basic of c++ programming
Basic of c++ programmingBasic of c++ programming
Basic of c++ programming
Talha Mughal
Intro
IntroIntro
Intro
CGC Technical campus,Mohali
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab new
eyavagal
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++
Mohamed El Desouki
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab new
Last7693
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab new
scottbrownnn
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptxKMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
JessylyneSophia
Computer programming all chapters
Computer programming all chaptersComputer programming all chapters
Computer programming all chapters
Ibrahim Elewah
ch01_an overview of computers and programming languages
ch01_an overview of computers and programming languagesch01_an overview of computers and programming languages
ch01_an overview of computers and programming languages
LiemLe21
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12
Terry Yoast
Fundamental of Programming Language UNIT-I
Fundamental of Programming Language UNIT-IFundamental of Programming Language UNIT-I
Fundamental of Programming Language UNIT-I
TruptiWable1
U19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptU19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).ppt
Manivannan837728
Debugging and Profiling C++ Template Metaprograms
Debugging and Profiling C++ Template MetaprogramsDebugging and Profiling C++ Template Metaprograms
Debugging and Profiling C++ Template Metaprograms
Platonov Sergey
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
Object oriented programming 12 programming steps in cpp and example
Object oriented programming 12 programming steps in cpp and exampleObject oriented programming 12 programming steps in cpp and example
Object oriented programming 12 programming steps in cpp and example
Vaibhav Khanna
COMP 122 Entire Course NEW
COMP 122 Entire Course NEWCOMP 122 Entire Course NEW
COMP 122 Entire Course NEW
shyamuopeight
Basic of c++ programming
Basic of c++ programmingBasic of c++ programming
Basic of c++ programming
Talha Mughal

Recently uploaded (20)

Why Hire Python Developers? Key Benefits for Your Business
Why Hire Python Developers? Key Benefits for Your BusinessWhy Hire Python Developers? Key Benefits for Your Business
Why Hire Python Developers? Key Benefits for Your Business
Mypcot Infotech
How John started to like TDD (instead of hating it) - TED talk
How John started to like TDD (instead of hating it) - TED talkHow John started to like TDD (instead of hating it) - TED talk
How John started to like TDD (instead of hating it) - TED talk
Nacho Cougil
iTop VPN Latest Version 2025 Crack Free Download
iTop VPN Latest Version 2025 Crack Free DownloadiTop VPN Latest Version 2025 Crack Free Download
iTop VPN Latest Version 2025 Crack Free Download
lr74xqnvuf
AVG Antivirus Crack With Free version Download 2025 [Latest]
AVG Antivirus Crack With Free version Download 2025 [Latest]AVG Antivirus Crack With Free version Download 2025 [Latest]
AVG Antivirus Crack With Free version Download 2025 [Latest]
haroonsaeed605
Rise of the Phoenix: Lesson Learned Build an AI-powered Test Gen Engine
Rise of the Phoenix: Lesson Learned Build an AI-powered Test Gen EngineRise of the Phoenix: Lesson Learned Build an AI-powered Test Gen Engine
Rise of the Phoenix: Lesson Learned Build an AI-powered Test Gen Engine
stevebrudz1
Advance Website Helpdesk Customer Support Ticket Management Odoo
Advance Website Helpdesk Customer Support Ticket Management OdooAdvance Website Helpdesk Customer Support Ticket Management Odoo
Advance Website Helpdesk Customer Support Ticket Management Odoo
Aagam infotech
Online Software Testing Training Institute in Delhi Ncr
Online Software Testing Training Institute in Delhi NcrOnline Software Testing Training Institute in Delhi Ncr
Online Software Testing Training Institute in Delhi Ncr
Home
Next-Gen Procurement: Leveraging AI for Smarter Sourcing & Cost Optimization
Next-Gen Procurement: Leveraging AI for Smarter Sourcing & Cost OptimizationNext-Gen Procurement: Leveraging AI for Smarter Sourcing & Cost Optimization
Next-Gen Procurement: Leveraging AI for Smarter Sourcing & Cost Optimization
asmith539880
Hire Odoo Developer OnestopDA Experts.
Hire Odoo Developer  OnestopDA Experts.Hire Odoo Developer  OnestopDA Experts.
Hire Odoo Developer OnestopDA Experts.
OnestopDA
LLM Security - Smart to protect, but too smart to be protected
LLM Security - Smart to protect, but too smart to be protectedLLM Security - Smart to protect, but too smart to be protected
LLM Security - Smart to protect, but too smart to be protected
Ivo Andreev
Tenorshare 4uKey Crack Fre e Download
Tenorshare  4uKey  Crack  Fre e DownloadTenorshare  4uKey  Crack  Fre e Download
Tenorshare 4uKey Crack Fre e Download
oyv9tzurtx
Douwan Preactivated Plus Crack 2025-Latest
Douwan Preactivated Plus Crack 2025-LatestDouwan Preactivated Plus Crack 2025-Latest
Douwan Preactivated Plus Crack 2025-Latest
mubeen010khan
Minitool Partition Wizard Crack Free Download
Minitool Partition Wizard Crack Free DownloadMinitool Partition Wizard Crack Free Download
Minitool Partition Wizard Crack Free Download
v3r2eptd2q
A Brief Introduction About Raman Bhaumik
A Brief Introduction About Raman BhaumikA Brief Introduction About Raman Bhaumik
A Brief Introduction About Raman Bhaumik
Raman Bhaumik
Build the future with Agentforce and Mulesoft
Build the future with Agentforce and  MulesoftBuild the future with Agentforce and  Mulesoft
Build the future with Agentforce and Mulesoft
GiulioPicchi
Wondershare Filmora Crack Free Download
Wondershare Filmora  Crack Free DownloadWondershare Filmora  Crack Free Download
Wondershare Filmora Crack Free Download
zqeevcqb3t
Metaverse Meetup: Explore Mulesoft MAC Project
Metaverse Meetup: Explore  Mulesoft MAC ProjectMetaverse Meetup: Explore  Mulesoft MAC Project
Metaverse Meetup: Explore Mulesoft MAC Project
GiulioPicchi
Wondershare Filmora 14.3.2 Crack + License Key Free Download
Wondershare Filmora 14.3.2 Crack + License Key Free DownloadWondershare Filmora 14.3.2 Crack + License Key Free Download
Wondershare Filmora 14.3.2 Crack + License Key Free Download
arshadkhokher01
Elastic Search Engineer Certification - Virtual
Elastic Search Engineer Certification - VirtualElastic Search Engineer Certification - Virtual
Elastic Search Engineer Certification - Virtual
Gon巽alo Pereira
Cybersecurity & Innovation: The Future of Mobile App Development
Cybersecurity & Innovation: The Future of Mobile App DevelopmentCybersecurity & Innovation: The Future of Mobile App Development
Cybersecurity & Innovation: The Future of Mobile App Development
iProgrammer Solutions Private Limited
Why Hire Python Developers? Key Benefits for Your Business
Why Hire Python Developers? Key Benefits for Your BusinessWhy Hire Python Developers? Key Benefits for Your Business
Why Hire Python Developers? Key Benefits for Your Business
Mypcot Infotech
How John started to like TDD (instead of hating it) - TED talk
How John started to like TDD (instead of hating it) - TED talkHow John started to like TDD (instead of hating it) - TED talk
How John started to like TDD (instead of hating it) - TED talk
Nacho Cougil
iTop VPN Latest Version 2025 Crack Free Download
iTop VPN Latest Version 2025 Crack Free DownloadiTop VPN Latest Version 2025 Crack Free Download
iTop VPN Latest Version 2025 Crack Free Download
lr74xqnvuf
AVG Antivirus Crack With Free version Download 2025 [Latest]
AVG Antivirus Crack With Free version Download 2025 [Latest]AVG Antivirus Crack With Free version Download 2025 [Latest]
AVG Antivirus Crack With Free version Download 2025 [Latest]
haroonsaeed605
Rise of the Phoenix: Lesson Learned Build an AI-powered Test Gen Engine
Rise of the Phoenix: Lesson Learned Build an AI-powered Test Gen EngineRise of the Phoenix: Lesson Learned Build an AI-powered Test Gen Engine
Rise of the Phoenix: Lesson Learned Build an AI-powered Test Gen Engine
stevebrudz1
Advance Website Helpdesk Customer Support Ticket Management Odoo
Advance Website Helpdesk Customer Support Ticket Management OdooAdvance Website Helpdesk Customer Support Ticket Management Odoo
Advance Website Helpdesk Customer Support Ticket Management Odoo
Aagam infotech
Online Software Testing Training Institute in Delhi Ncr
Online Software Testing Training Institute in Delhi NcrOnline Software Testing Training Institute in Delhi Ncr
Online Software Testing Training Institute in Delhi Ncr
Home
Next-Gen Procurement: Leveraging AI for Smarter Sourcing & Cost Optimization
Next-Gen Procurement: Leveraging AI for Smarter Sourcing & Cost OptimizationNext-Gen Procurement: Leveraging AI for Smarter Sourcing & Cost Optimization
Next-Gen Procurement: Leveraging AI for Smarter Sourcing & Cost Optimization
asmith539880
Hire Odoo Developer OnestopDA Experts.
Hire Odoo Developer  OnestopDA Experts.Hire Odoo Developer  OnestopDA Experts.
Hire Odoo Developer OnestopDA Experts.
OnestopDA
LLM Security - Smart to protect, but too smart to be protected
LLM Security - Smart to protect, but too smart to be protectedLLM Security - Smart to protect, but too smart to be protected
LLM Security - Smart to protect, but too smart to be protected
Ivo Andreev
Tenorshare 4uKey Crack Fre e Download
Tenorshare  4uKey  Crack  Fre e DownloadTenorshare  4uKey  Crack  Fre e Download
Tenorshare 4uKey Crack Fre e Download
oyv9tzurtx
Douwan Preactivated Plus Crack 2025-Latest
Douwan Preactivated Plus Crack 2025-LatestDouwan Preactivated Plus Crack 2025-Latest
Douwan Preactivated Plus Crack 2025-Latest
mubeen010khan
Minitool Partition Wizard Crack Free Download
Minitool Partition Wizard Crack Free DownloadMinitool Partition Wizard Crack Free Download
Minitool Partition Wizard Crack Free Download
v3r2eptd2q
A Brief Introduction About Raman Bhaumik
A Brief Introduction About Raman BhaumikA Brief Introduction About Raman Bhaumik
A Brief Introduction About Raman Bhaumik
Raman Bhaumik
Build the future with Agentforce and Mulesoft
Build the future with Agentforce and  MulesoftBuild the future with Agentforce and  Mulesoft
Build the future with Agentforce and Mulesoft
GiulioPicchi
Wondershare Filmora Crack Free Download
Wondershare Filmora  Crack Free DownloadWondershare Filmora  Crack Free Download
Wondershare Filmora Crack Free Download
zqeevcqb3t
Metaverse Meetup: Explore Mulesoft MAC Project
Metaverse Meetup: Explore  Mulesoft MAC ProjectMetaverse Meetup: Explore  Mulesoft MAC Project
Metaverse Meetup: Explore Mulesoft MAC Project
GiulioPicchi
Wondershare Filmora 14.3.2 Crack + License Key Free Download
Wondershare Filmora 14.3.2 Crack + License Key Free DownloadWondershare Filmora 14.3.2 Crack + License Key Free Download
Wondershare Filmora 14.3.2 Crack + License Key Free Download
arshadkhokher01
Elastic Search Engineer Certification - Virtual
Elastic Search Engineer Certification - VirtualElastic Search Engineer Certification - Virtual
Elastic Search Engineer Certification - Virtual
Gon巽alo Pereira

UoN-Lec_12_Control_Structure.pdf

  • 1. Programming Fundamentals By Adnan Waheed University of Narowal Control Structure (Selection Statements) Lecture No. 12 Tuesday, November 21, 2023
  • 2. Avoiding Bugs by Avoiding Partially Understood Concepts and Techniques Must use concepts and techniques correctly; Otherwise solution will be either incorrect or deficient If you do not understand a concept or technique completely Dont use it Save yourself an enormous amount of debugging time C++ Programming: Program Design Including Data Structures, Fifth Edition 2
  • 3. Input Failure and the if Statement If input stream enters a fail state All subsequent input statements associated with that stream are ignored Program continues to execute May produce erroneous results Can use if statements to check status of input stream If stream enters the fail state, include instructions that stop program execution C++ Programming: Program Design Including Data Structures, Fifth Edition 3
  • 4. Confusion Between the Equality (==) and Assignment (=) Operators 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 C++ Programming: Program Design Including Data Structures, Fifth Edition 4
  • 5. Program Style and Form (Revisited): Indentation If your program is properly indented Spot and fix errors quickly Show the natural grouping of statements Insert a blank line between statements that are naturally separate Two commonly used styles for placing braces On a line by themselves Or left brace is placed after the expression, and the right brace is on a line by itself C++ Programming: Program Design Including Data Structures, Fifth Edition 5
  • 6. Using Pseudocode to Develop, Test, and Debug a Program Pseudocode, or just pseudo Informal mixture of C++ and ordinary language Helps you quickly develop the correct structure of the program and avoid making common errors Use a wide range of values in a walk- through to evaluate the program C++ Programming: Program Design Including Data Structures, Fifth Edition 6
  • 7. 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 C++ Programming: Program Design Including Data Structures, Fifth Edition 7
  • 8. switch Structures Syntax switch(expression) { case x: // code block break; case y: // code block break; default: // code block C++ Programming: Program Design Including Data Structures, Fifth Edition 8 The switch expression is evaluated once The value of the expression is compared with the values of each case If there is a match, the associated block of code is executed The break and default keywords are optional
  • 9. switch Structures (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition 9
  • 10. Switch Example The example below uses the weekday number to calculate the weekday name: int day = 4; switch (day) { case 1: cout << "Monday"; break; case 2: cout << "Tuesday"; break; case 3: cout << "Wednesday"; break; case 4: cout << "Thursday"; break; case 5: cout << "Friday"; break; case 6: cout << "Saturday"; break; case 7: cout << "Sunday"; break; // Outputs "Thursday" (day 4) C++ Programming: Program Design Including Data Structures, Fifth Edition 10
  • 11. The break Keyword C++ Programming: Program Design Including Data Structures, Fifth Edition 11 When C++ reaches a break keyword, it breaks out of the switch block. This will stop the execution of more code and case testing inside the block. When a match is found, and the job is done, it's time for a break. There is no need for more testing.
  • 12. The default Keyword C++ Programming: Program Design Including Data Structures, Fifth Edition 12 The default keyword specifies some code to run if there is no case match: int day = 4; switch (day) { case 6: cout << "Today is Saturday"; break; case 7: cout << "Today is Sunday"; break; default: cout << "Looking forward to the Weekend"; } // Outputs "Looking forward to the Weekend.
  • 13. switch Structures (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition 13
  • 14. C++ Programming: Program Design Including Data Structures, Fifth Edition 14
  • 15. Output = ? C++ Programming: Program Design Including Data Structures, Fifth Edition 15
  • 16. Avoiding Bugs by Avoiding Partially Understood Concepts and Techniques: Revisited To output results correctly The switch structure must include a break statement after each cout statement C++ Programming: Program Design Including Data Structures, Fifth Edition 16
  • 17. COMPLETE EXAMPLE @ HOME FOR PRACTICE OF STUDENTS C++ Programming: Program Design Including Data Structures, Fifth Edition 17
  • 18. Programming Example: Cable Company Billing This programming example calculates a customers bill for a local cable company There are two types of customers: Residential Business Two rates for calculating a cable bill: One for residential customers One for business customers C++ Programming: Program Design Including Data Structures, Fifth Edition 18
  • 19. Programming Example: Rates For residential customer: Bill processing fee: $4.50 Basic service fee: $20.50 Premium channel: $7.50 per channel For business customer: Bill processing fee: $15.00 Basic service fee: $75.00 for first 10 connections/$5.00 for each additional one Premium channel cost: $50.00 per channel for any number of connections C++ Programming: Program Design Including Data Structures, Fifth Edition 19
  • 20. Programming Example: Requirements Ask user for account number and customer code Assume R or r stands for residential customer and B or b stands for business customer C++ Programming: Program Design Including Data Structures, Fifth Edition 20
  • 21. Programming Example: Input and Output Input: Customer account number Customer code Number of premium channels For business customers, number of basic service connections Output: Customers account number Billing amount C++ Programming: Program Design Including Data Structures, Fifth Edition 21
  • 22. Programming Example: Program Analysis Purpose: calculate and print billing amount Calculating billing amount requires: Customer for whom the billing amount is calculated (residential or business) Number of premium channels to which the customer subscribes For a business customer, you need: Number of basic service connections Number of premium channels C++ Programming: Program Design Including Data Structures, Fifth Edition 22
  • 23. Programming Example: Program Analysis (cont'd.) Data needed to calculate the bill, such as bill processing fees and the cost of a premium channel, are known quantities The program should print the billing amount to two decimal places C++ Programming: Program Design Including Data Structures, Fifth Edition 23
  • 24. Programming Example: Algorithm Design Set precision to two decimal places Prompt user for account number and customer type If customer type is R or r Prompt user for number of premium channels Compute and print the bill If customer type is B or b Prompt user for number of basic service connections and number of premium channels Compute and print the bill C++ Programming: Program Design Including Data Structures, Fifth Edition 24
  • 25. Programming Example: Variables and Named Constants C++ Programming: Program Design Including Data Structures, Fifth Edition 25
  • 26. Programming Example: Formulas Billing for residential customers: amountDue = RES_BILL_PROC_FEES + RES_BASIC_SERV_COST + numOfPremChannels * RES_COST_PREM_CHANNEL; C++ Programming: Program Design Including Data Structures, Fifth Edition 26
  • 27. Programming Example: Formulas (cont'd.) Billing for business customers: if (numOfBasicServConn <= 10) amountDue = BUS_BILL_PROC_FEES + BUS_BASIC_SERV_COST + numOfPremChannels * BUS_COST_PREM_CHANNEL; else amountDue = BUS_BILL_PROC_FEES + BUS_BASIC_SERV_COST + (numOfBasicServConn - 10) * BUS_BASIC_CONN_COST + numOfPremChannels * BUS_COST_PREM_CHANNEL; C++ Programming: Program Design Including Data Structures, Fifth Edition 27
  • 28. Programming Example: Main Algorithm 1. Output floating-point numbers in fixed decimal with decimal point and trailing zeros Output floating-point numbers with two decimal places and set the precision to two decimal places 2. Prompt user to enter account number 3. Get customer account number 4. Prompt user to enter customer code 5. Get customer code C++ Programming: Program Design Including Data Structures, Fifth Edition 28
  • 29. Programming Example: Main Algorithm (cont'd.) 6. If the customer code is r or R, Prompt user to enter number of premium channels Get the number of premium channels Calculate the billing amount Print account number and billing amount C++ Programming: Program Design Including Data Structures, Fifth Edition 29
  • 30. Programming Example: Main Algorithm (cont'd.) 7. If customer code is b or B, Prompt user to enter number of basic service connections Get number of basic service connections Prompt user to enter number of premium channels Get number of premium channels Calculate billing amount Print account number and billing amount C++ Programming: Program Design Including Data Structures, Fifth Edition 30
  • 31. Programming Example: Main Algorithm (cont'd.) 8. If customer code is other than r, R, b, or B, output an error message C++ Programming: Program Design Including Data Structures, Fifth Edition 31
  • 32. 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) C++ Programming: Program Design Including Data Structures, Fifth Edition 32
  • 33. Summary (cont'd.) Two selection structures: one-way selection and two-way selection The expression in an if or if...else structure is usually a logical expression 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 C++ Programming: Program Design Including Data Structures, Fifth Edition 33
  • 34. Summary (cont'd.) Using assignment in place of the equality operator creates a semantic error switch structure handles multiway selection break statement ends switch statement Use assert to terminate a program if certain conditions are not met C++ Programming: Program Design Including Data Structures, Fifth Edition 34