© 1999, by Que Education and Training, Chapter 5, pages 225-257 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Select Case Statements and Selection Input.
Advertisements

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Programming Logic and Design Eighth Edition
© 1999, by Que Education and Training, Chapter 6, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
Comparing Numeric Values If Val(Text1.Text) = MaxPrice Then (Is the current numeric value stored in the Text property of Text1 equal to the value stored.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 4 Decisions and Conditions.
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS.
Dani Vainstein1 VBScript Session 9. Dani Vainstein2 What we learn last session? VBScript coding conventions. Code convention usage for constants, variables,
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Tutorial 11 Using and Writing Visual Basic for Applications Code
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
Chapter 4: The Selection Process in Visual Basic.
Chapter 12: How Long Can This Go On?
Chapter 3 Making Decisions
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf.
Chapter 4 Selection Structures: Making Decisions.
Flow of Control Part 1: Selection
Tutorial 51 Programming Structures Sequence - program instructions are processed, one after another, in the order in which they appear in the program Selection.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Security Panel Application Introducing the Select Case Multiple-Selection Statement.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Programming with Microsoft Visual Basic th Edition
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!!
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Chapter Making Decisions 4. Relational Operators 4.1.
Pay Example (PFirst98) Please use speaker notes for additional information!
CHAPTER 6 LESSON B Creating Custom Forms. Lesson B Objectives  Suppress default system messages  Create alerts and messages to provide system feedback.
For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5.
31/01/ Selection If selection construct.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Controlling Program Flow with Decision Structures.
Microsoft Visual Basic 2012 CHAPTER FIVE Decision Structures.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Creation of Variables with Numeric, alphanumeric, date, picture, memo data types Constant - A quantity that does not change during the execution of a program.
Visual Basic/ Visual Studio Brandon Large. Connecting to prior knowledge In your notes write down what the two main parts of the computer are. The “software”
Computer Science Up Down Controls, Decisions and Random Numbers.
Selection Using IF THEN ELSE CASE Introducing Loops.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 4: Making Decisions.
Javascript Conditionals.
CHAPTER FIVE Decision Structures.
Tutorial 12 – Security Panel Application Introducing the Select Case Multiple-Selection Statement Outline Test-Driving the Security Panel Application.
Department Array in Visual Basic
Conditions and Ifs BIS1523 – Lecture 8.
Lesson 04 Control Structures I : Decision Making
Part A – Doing Your Own Input Validation with Simple VB Tools
Decisions and Conditions
CIS 16 Application Development Programming with Visual Basic
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 3: Selection Structures: Making Decisions
Introduction to Computer Programming IT-104
Presentation transcript:

© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach When are decisions used? n Give user greater control i Skip processing that doesn’t apply to user’s situation u Ex: A questionnaire that asks gender & pregnant i Select processing as requested by user u Ex: An investment program to find either the present value or future value n Data validation n Finding minimums and maximums

© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Simple Decisions If condition Then ‘ statement(s) End If If condition Then ‘ statement(s) for true Else ‘ statement(s) for false End If Condition ? TrueFalse Steps to process if condition is true Condition ? TrueFalse Steps to process if condition is true Steps to process if condition is false

© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Relational Operators Used to compare two values in a simple condition Comparing Strings: ASCII Collating Sequence (tear card) Use caution when comparing the text property of text boxes (as a variant, it may be considered a string or number, depending upon what it is being compared to)

© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Generalized Approach for Data Validation Function n Assume there will be a problem i Assign invalid return value to function n For each data value input by user, verify the correctness of the data prior to processing i If data is invalid, do the following: u Display message to user to specifying problem u Force user to re-enter data u Exit function n If all input checks were successful i Assign valid return value to function

© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Logical Operators Used to form compound (multiple) conditions

© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Logical Expressions for Validation n Check the invalid cases in the condition i Make sure that salary is between $500 and $2500: If Not (Salary >= 500 And Salary <= 2500) Then ‘ do error processing since outside range limits End If is equivalent to If Salary 2500 Then ‘ do error processing since outside range limits End If n When negating a complex condition, both the relational operators and logical operators must be reversed

© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Logical Expressions for Validation n Check the invalid cases in the condition n Make sure that age is between 21 and 75: If CInt(txtAge.Text) < 21 OR _ CInt(txtAge.Text) > 75 Then ‘ do error processing since outside range limits End If n Make sure single people have no spouse If txtStatus.Text = “single” AND _ txtSpouse.Text <> “” Then ‘ do error processing - spouse given for single person End If

© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Types of Validation Checks n Existence Check n Type Check n Range Check n Reasonableness Check n Code Check n Consistency Check

© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Existence Check n Used to verify that required data was input n AKA presence check n Compare the input value against the empty string. n Example: If last name is required input, use: If txtLast.Text = “” Then ‘ error processing End If

© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Type Check n Used to verify that the type of data input is correct n IsNumeric & IsDate functions i Verifies that input data consists only of valid numbers or legal dates n Example: If salary is a valid number: If Not IsNumeric(txtSalary.Text) Then ‘ error processing End If

© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Range Check n Used to verify that required data falls within valid limits n Compare the input value against the absolute end points n Example: If age must be positive: If CInt(txtAge.Text) <= 0 Then ‘ error processing End If

© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Reasonableness Check n Special case of range check n Used to verify that required data falls within expected limits n Example: If age is unlikely to be under 21: If CInt(txtAge.Text) < 21 Then ‘ error processing - warn user on possible problem End If

© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Code Check n Similar to range check n Look for specific matches (special codes) n Example: If the first two characters of employee ID must be 19: If Left(txtID.Text, 2) <> “19” Then ‘ error processing End If

© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Consistency Check n Used to verify that multiple input data does not establish invalid relationships n Examples: i Hospital bill processing program confirms that labor and delivery charges are for a female patient. i A single employee does not list a spouse. i A 12-year old cannot rent an adult video. n Multiple conditions checked in same condition

© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Displaying messages to user n MsgBox function i Displays modal dialog box to user i Waits for user to click button i Returns integer indicating which button was clicked

© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach MsgBox Function n Variable = MsgBox(MsgString, TypeOfWindow) i MsgString is a customized string that appears in the dialog window i TypeOfWindow identifies the icons & buttons displayed u vbOKOnly + vbInformation u vbYesNo + vbQuestion + vbDefaultButton2 n Displays a message & returns value of button user pressed. n Example: Response = MsgBox(“Do you want to continue?”, _ vbYesNo + vbQuestion + vbDefaultButton2) If Response = vbYes Then ‘ continue processing Else ‘ terminate processing EndIf

© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach MsgBox “Statement” n Displays a message, but does not keep track of which button user pressed. i Call MsgBox(MsgString, TypeOfWindow) n Example: If Not IsNumeric(txtAge.Text) Then Call MsgBox(“Age must be numeric. Please re-enter.”, _ vbOkOnly + vbInformation) EndIf

© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Force user to re-enter data n Change focus to erroneous textbox Call txtName.SetFocus n Highlight text in textbox txtName.SelStart = 0 txtName.SelLength = Len(txtName.Text)

© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach When to do data validation? n Text box’s Change event i Triggered on every minute change n Text box’s LostFocus event i Triggered immediately upon moving the focus out of a text box i Cascading events n Calculate button’s Click event i Call data validation function & bypass calculate processing if return value indicates invalid input

© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Data Validation Example 1 Private Sub cmdCalc_Click() If DataVal Then ‘ do normal processing End If End Sub Private Function DataVal() As Boolean DataVal = False ‘ default return value assumes a problem If Not IsNumeric(txtHours.Text) Then Call MsgBox(“Hours must be numeric-Please re-enter.”, _ vbOkOnly + vbInformation) Call txtHours.SetFocus Exit Function End If ‘ repeat similar decision structure for remaining input text boxes DataVal = True ‘ change return value since no problems discovered End Function

© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Data Validation Example 2 Private Sub cmdCalc_Click() If DataVal = False Then Exit Sub End If ‘ do normal processing End Sub Private Function DataVal() As Boolean DataVal = False ‘ default return value assumes a problem If Not IsNumeric(txtHours.Text) Then Call MsgBox(“Hours must be numeric-Please re-enter.”, _ vbOkOnly + vbInformation) Call txtHours.SetFocus Exit Function End If ‘ repeat similar decision structure for remaining input text boxes DataVal = True ‘ change return value since no problems discovered End Function

© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Minimums and Maximums n Generalized procedure is similar to the counting/accumulating process i Use higher-scope variable i Initialize before processing - two choices u In Form_Load assign maximum to smallest possible value and minimum to largest possible value u During processing, assign minimum and maximum to the first value –This requires special steps to track when the first value is processed i During processing, compare to determine if there is a new minimum or maximum

© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Program Testing Guidelines n Adding selective structures to your program requires extra steps for thorough program testing n Construct sample input data with provisions for each of the following: i Execute all statements at least once i Execute all branches of conditions i Enter different lengths of string input i Enter various types of numeric input i Test common error situations i Repeat the steps in a different order