SlideShare a Scribd company logo
1 of 18
CSWD1001
Programming Methods
Input validation
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
1
What we’ll learn
• Garbage in, garbage out
• The input validation loop
• Defensive programming
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
2
Bad Data – Garbage In, Garbage Out
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
3
Characteristics of a Program
• If a user provides bad data as input to a program, the program will
process that bad data and, as a result, will produce bad data as output.
• As computers cannot tell the difference between good data and bad
data.
• Programs should be designed to reject bad data that is given as input.
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
4
Example
// Variables to hold the hours worked, the
// hourly pay rate, and the gross pay.
Declare Real hours, payRate, grossPay
// Get the number of hours worked.
Display "Enter the number of hours worked."
Input hours
// Get the hourly pay rate.
Display "Enter the hourly pay rate."
Input payRate
// Calculate the gross pay.
Set grossPay = hours * payRate
// Display the gross pay.
Display "The gross pay is ", currencyFormat(grossPay)
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
5
The Input Validation Loops
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
6
Concept
• Input validation is commonly done with a loop that iterates as
long as an input variable contains bad data.
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
7
Logic Containing An Input Validation Loop
Get input
If input
invalid?
Display an
error message
Get the input
again
True
False
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
8
Example Use Case Designing an Input
Validation Loop
• Previous example of Samantha: calculate the retail price of an item in her import
business.
• Samantha has encountered a problem when using the program, however. Some of
the items that she sells have a wholesale cost of 50 cents, which she enters into the
program as 0.50.
• Because the 0 key is next to the key for the negative sign, she sometimes
accidentally enters a negative number. She has asked you to revise the program so
it will not allow a negative number to be entered for the wholesale cost.
• You decide to add an input validation loop to the showRetail module that rejects
any negative numbers that are entered into the wholesale variable.
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
9
Pseudocode
Module main()
// Local variable
Declare String doAnother
Do
// Calculate and display a retail price.
Call showRetail()
// Do this again?
Display "Do you have another item?
(Enter y for yes)"
Input doAnother
While doAnother == "y" OR doAnother == "Y"
End Module
// The showRetail module gets an item's wholesale cost
// from the user and displays its retail price.
Module showRetail()
// Local variables
Declare Real wholesale, retail
// Constant for the markup percentage
Constant Real MARKUP = 2.50
// Get the wholesale cost.
Display "Enter an item's wholesale cost."
Input wholesale
// Validate the wholesale cost.
While wholesale < 0
Display "The cost cannot be negative.
Please enter the correct wholesale cost."
Input wholesale
End While
// Calculate the retail price.
Set retail = wholesale * MARKUP
// Display the retail price.
Display "The retail price is $", retail
End Module
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
10
Flowchart
showRetail()
Declare
Real wholesale, retail
Constant MARKUP = 2.50
Display “Enter an
item’s wholesale cost”
Input wholesale
wholesale
< 0
Set retail = wholesale * MARKUP
Display “The cost
cannot be negative.
Please enter the correct
wholesale cost”
Input wholesale
Display “The retail price
is $”, retail Return
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
11
Writing Validation Functions
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
12
• Input validation can sometimes be more complex
• Validation - put inside a function
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
13
Example
// Get the model number.
Display "Enter the model number."
Input model
While isInvalid(model)
Display "The valid model numbers are 100, 200, and 300."
Display "Enter a valid model number."
Input model
End While
Function Boolean isInvalid(Integer model)
// A local variable to hold True or False.
Declare Boolean status
// If the model number is invalid, set status to True.
// Otherwise, set status to False.
If model != 100 AND model != 200 AND model != 300 Then
Set status = True
Else
Set status = False
End If
// Return the test status.
Return status
End Function
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
14
Defensive Programming
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
15
Concept
• Defensive programming is the practice of anticipating errors that can
happen while a program is running, and designing the program to
avoid those errors.
• Input validation is part of the practice of defensive programming.
Thorough input validation anticipates both obvious and unobvious
errors.
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
16
Common Application Usage
• Country address, number
• Hourly wages, salary
• Dates
• Time, temperature
• Etc….
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
17
Summary
• Programs should be designed to reject bad data that is given as input.
• Input validation is commonly done with a loop that iterates as long as
an input variable contains bad data.
• Input validation can sometimes be more complex. Validation - put
inside a function
• Defensive programming is the practice of anticipating errors that can
happen while a program is running, and designing the program to
avoid those errors.
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
18

More Related Content

Similar to 7 Input Validation

Target Costing and PricingExercise 8 Lovebug Company has de.docx
Target Costing and PricingExercise 8 Lovebug Company has de.docxTarget Costing and PricingExercise 8 Lovebug Company has de.docx
Target Costing and PricingExercise 8 Lovebug Company has de.docx
josies1
 
Question 1 of 205.0 PointsA system that focuses on activities .docx
Question 1 of 205.0 PointsA system that focuses on activities .docxQuestion 1 of 205.0 PointsA system that focuses on activities .docx
Question 1 of 205.0 PointsA system that focuses on activities .docx
IRESH3
 
PT1420 Modules in Flowchart and Visual Basic .docx
PT1420 Modules in Flowchart and Visual Basic             .docxPT1420 Modules in Flowchart and Visual Basic             .docx
PT1420 Modules in Flowchart and Visual Basic .docx
amrit47
 

Similar to 7 Input Validation (20)

5 Repetition Structures
5 Repetition Structures5 Repetition Structures
5 Repetition Structures
 
2 Program Design Methodology
2 Program Design Methodology2 Program Design Methodology
2 Program Design Methodology
 
8 Array
8 Array8 Array
8 Array
 
3 Modules
3 Modules3 Modules
3 Modules
 
1 Introduction to Computers and Programming Language
1 Introduction to Computers and Programming Language1 Introduction to Computers and Programming Language
1 Introduction to Computers and Programming Language
 
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
 
Data Science Salon Miami Example - Churn Rate Predictor
Data Science Salon Miami Example - Churn Rate PredictorData Science Salon Miami Example - Churn Rate Predictor
Data Science Salon Miami Example - Churn Rate Predictor
 
COMP 122 Entire Course NEW
COMP 122 Entire Course NEWCOMP 122 Entire Course NEW
COMP 122 Entire Course NEW
 
C# Tutorial MSM_Murach chapter-05-slides
C# Tutorial MSM_Murach chapter-05-slidesC# Tutorial MSM_Murach chapter-05-slides
C# Tutorial MSM_Murach chapter-05-slides
 
Super Sure MCQs on ManagementCommerce PART 8.pdf
Super Sure MCQs on ManagementCommerce PART 8.pdfSuper Sure MCQs on ManagementCommerce PART 8.pdf
Super Sure MCQs on ManagementCommerce PART 8.pdf
 
Target Costing and PricingExercise 8 Lovebug Company has de.docx
Target Costing and PricingExercise 8 Lovebug Company has de.docxTarget Costing and PricingExercise 8 Lovebug Company has de.docx
Target Costing and PricingExercise 8 Lovebug Company has de.docx
 
Question 1 of 205.0 PointsA system that focuses on activities .docx
Question 1 of 205.0 PointsA system that focuses on activities .docxQuestion 1 of 205.0 PointsA system that focuses on activities .docx
Question 1 of 205.0 PointsA system that focuses on activities .docx
 
Scaling AutoML-Driven Anomaly Detection With Luminaire
Scaling AutoML-Driven Anomaly Detection With LuminaireScaling AutoML-Driven Anomaly Detection With Luminaire
Scaling AutoML-Driven Anomaly Detection With Luminaire
 
Cis 115 Education Redefined-snaptutorial.com
Cis 115 Education Redefined-snaptutorial.comCis 115 Education Redefined-snaptutorial.com
Cis 115 Education Redefined-snaptutorial.com
 
Building rich domain models with ddd and tdd ivan paulovich - betsson
Building rich domain models with ddd and tdd   ivan paulovich - betssonBuilding rich domain models with ddd and tdd   ivan paulovich - betsson
Building rich domain models with ddd and tdd ivan paulovich - betsson
 
PT1420 Modules in Flowchart and Visual Basic .docx
PT1420 Modules in Flowchart and Visual Basic             .docxPT1420 Modules in Flowchart and Visual Basic             .docx
PT1420 Modules in Flowchart and Visual Basic .docx
 
Fueling DevOps with a Testing Trifecta: How the New World of Testing is Driv...
Fueling DevOps with a Testing Trifecta:  How the New World of Testing is Driv...Fueling DevOps with a Testing Trifecta:  How the New World of Testing is Driv...
Fueling DevOps with a Testing Trifecta: How the New World of Testing is Driv...
 
Credit Card Fraud Detection Using Machine Learning & Data Science
Credit Card Fraud Detection Using Machine Learning & Data ScienceCredit Card Fraud Detection Using Machine Learning & Data Science
Credit Card Fraud Detection Using Machine Learning & Data Science
 
Credit Card Fraud Detection Using Machine Learning & Data Science
Credit Card Fraud Detection Using Machine Learning & Data ScienceCredit Card Fraud Detection Using Machine Learning & Data Science
Credit Card Fraud Detection Using Machine Learning & Data Science
 
Topic 5 - Costing Method_CT.ppt
Topic 5 -  Costing Method_CT.pptTopic 5 -  Costing Method_CT.ppt
Topic 5 - Costing Method_CT.ppt
 

Recently uploaded

會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 

Recently uploaded (20)

24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
“O BEIJO” EM ARTE .
“O BEIJO” EM ARTE                       .“O BEIJO” EM ARTE                       .
“O BEIJO” EM ARTE .
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
Implanted Devices - VP Shunts: EMGuidewire's Radiology Reading Room
Implanted Devices - VP Shunts: EMGuidewire's Radiology Reading RoomImplanted Devices - VP Shunts: EMGuidewire's Radiology Reading Room
Implanted Devices - VP Shunts: EMGuidewire's Radiology Reading Room
 
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
The Ball Poem- John Berryman_20240518_001617_0000.pptx
The Ball Poem- John Berryman_20240518_001617_0000.pptxThe Ball Poem- John Berryman_20240518_001617_0000.pptx
The Ball Poem- John Berryman_20240518_001617_0000.pptx
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
How to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 InventoryHow to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 Inventory
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 

7 Input Validation

  • 1. CSWD1001 Programming Methods Input validation 18/9/2018 CSWD1001 @ Kwan Lee First City Unversity Malaysia (FCUC) 1
  • 2. What we’ll learn • Garbage in, garbage out • The input validation loop • Defensive programming 18/9/2018 CSWD1001 @ Kwan Lee First City Unversity Malaysia (FCUC) 2
  • 3. Bad Data – Garbage In, Garbage Out 18/9/2018 CSWD1001 @ Kwan Lee First City Unversity Malaysia (FCUC) 3
  • 4. Characteristics of a Program • If a user provides bad data as input to a program, the program will process that bad data and, as a result, will produce bad data as output. • As computers cannot tell the difference between good data and bad data. • Programs should be designed to reject bad data that is given as input. 18/9/2018 CSWD1001 @ Kwan Lee First City Unversity Malaysia (FCUC) 4
  • 5. Example // Variables to hold the hours worked, the // hourly pay rate, and the gross pay. Declare Real hours, payRate, grossPay // Get the number of hours worked. Display "Enter the number of hours worked." Input hours // Get the hourly pay rate. Display "Enter the hourly pay rate." Input payRate // Calculate the gross pay. Set grossPay = hours * payRate // Display the gross pay. Display "The gross pay is ", currencyFormat(grossPay) 18/9/2018 CSWD1001 @ Kwan Lee First City Unversity Malaysia (FCUC) 5
  • 6. The Input Validation Loops 18/9/2018 CSWD1001 @ Kwan Lee First City Unversity Malaysia (FCUC) 6
  • 7. Concept • Input validation is commonly done with a loop that iterates as long as an input variable contains bad data. 18/9/2018 CSWD1001 @ Kwan Lee First City Unversity Malaysia (FCUC) 7
  • 8. Logic Containing An Input Validation Loop Get input If input invalid? Display an error message Get the input again True False 18/9/2018 CSWD1001 @ Kwan Lee First City Unversity Malaysia (FCUC) 8
  • 9. Example Use Case Designing an Input Validation Loop • Previous example of Samantha: calculate the retail price of an item in her import business. • Samantha has encountered a problem when using the program, however. Some of the items that she sells have a wholesale cost of 50 cents, which she enters into the program as 0.50. • Because the 0 key is next to the key for the negative sign, she sometimes accidentally enters a negative number. She has asked you to revise the program so it will not allow a negative number to be entered for the wholesale cost. • You decide to add an input validation loop to the showRetail module that rejects any negative numbers that are entered into the wholesale variable. 18/9/2018 CSWD1001 @ Kwan Lee First City Unversity Malaysia (FCUC) 9
  • 10. Pseudocode Module main() // Local variable Declare String doAnother Do // Calculate and display a retail price. Call showRetail() // Do this again? Display "Do you have another item? (Enter y for yes)" Input doAnother While doAnother == "y" OR doAnother == "Y" End Module // The showRetail module gets an item's wholesale cost // from the user and displays its retail price. Module showRetail() // Local variables Declare Real wholesale, retail // Constant for the markup percentage Constant Real MARKUP = 2.50 // Get the wholesale cost. Display "Enter an item's wholesale cost." Input wholesale // Validate the wholesale cost. While wholesale < 0 Display "The cost cannot be negative. Please enter the correct wholesale cost." Input wholesale End While // Calculate the retail price. Set retail = wholesale * MARKUP // Display the retail price. Display "The retail price is $", retail End Module 18/9/2018 CSWD1001 @ Kwan Lee First City Unversity Malaysia (FCUC) 10
  • 11. Flowchart showRetail() Declare Real wholesale, retail Constant MARKUP = 2.50 Display “Enter an item’s wholesale cost” Input wholesale wholesale < 0 Set retail = wholesale * MARKUP Display “The cost cannot be negative. Please enter the correct wholesale cost” Input wholesale Display “The retail price is $”, retail Return 18/9/2018 CSWD1001 @ Kwan Lee First City Unversity Malaysia (FCUC) 11
  • 12. Writing Validation Functions 18/9/2018 CSWD1001 @ Kwan Lee First City Unversity Malaysia (FCUC) 12
  • 13. • Input validation can sometimes be more complex • Validation - put inside a function 18/9/2018 CSWD1001 @ Kwan Lee First City Unversity Malaysia (FCUC) 13
  • 14. Example // Get the model number. Display "Enter the model number." Input model While isInvalid(model) Display "The valid model numbers are 100, 200, and 300." Display "Enter a valid model number." Input model End While Function Boolean isInvalid(Integer model) // A local variable to hold True or False. Declare Boolean status // If the model number is invalid, set status to True. // Otherwise, set status to False. If model != 100 AND model != 200 AND model != 300 Then Set status = True Else Set status = False End If // Return the test status. Return status End Function 18/9/2018 CSWD1001 @ Kwan Lee First City Unversity Malaysia (FCUC) 14
  • 15. Defensive Programming 18/9/2018 CSWD1001 @ Kwan Lee First City Unversity Malaysia (FCUC) 15
  • 16. Concept • Defensive programming is the practice of anticipating errors that can happen while a program is running, and designing the program to avoid those errors. • Input validation is part of the practice of defensive programming. Thorough input validation anticipates both obvious and unobvious errors. 18/9/2018 CSWD1001 @ Kwan Lee First City Unversity Malaysia (FCUC) 16
  • 17. Common Application Usage • Country address, number • Hourly wages, salary • Dates • Time, temperature • Etc…. 18/9/2018 CSWD1001 @ Kwan Lee First City Unversity Malaysia (FCUC) 17
  • 18. Summary • Programs should be designed to reject bad data that is given as input. • Input validation is commonly done with a loop that iterates as long as an input variable contains bad data. • Input validation can sometimes be more complex. Validation - put inside a function • Defensive programming is the practice of anticipating errors that can happen while a program is running, and designing the program to avoid those errors. 18/9/2018 CSWD1001 @ Kwan Lee First City Unversity Malaysia (FCUC) 18

Editor's Notes

  1. The input is read, and then a pretest loop is executed. If the input data is invalid the body of the loop executes. The loop displays an error message so the user will know that the input was invalid, and then the loop reads the new input. The loop repeats as long as the input is invalid.