Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf.

Slides:



Advertisements
Similar presentations
1.
Advertisements

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
Selection (decision) control structure Learning objective
Microsoft Visual Basic: Reloaded Chapter Five More on the Selection Structure.
Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
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.
Copyright © 2001 by Wiley. All rights reserved. Chapter 8: Security, Menus, and Files Security Menu Systems Creating a Memo Editor Using the Clipboard.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Adding Automated Functionality to Office Applications.
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid.
Chapter 4: The Selection Structure
Microsoft Excel 2007 © Wiley Publishing All Rights Reserved. The L Line The Express Line to Learning L Line.
Chapter 4 The If…Then Statement
Chapter 11: Introduction to the Visual Basic Environment Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
Chapter 4: The Selection Process in Visual Basic.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Wage Calculator Application.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
Lecture Set 5 Control Structures Part A - Decisions Structures.
Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops.
Copyright © 2001 by Wiley. All rights reserved. Chapter 2: Using Visual Basic to Create a First Project Getting Started with VB Development Environment.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
CHAPTER FIVE Specifying Alternate Courses of Action: Selection Statements.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 5: More on the Selection Structure
Programming with Microsoft Visual Basic th Edition
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
4-1 Chapter 4 The Selection Process in VB.NET. 4-2 Learning Objectives Understand the importance of the selection process in programming. Describe the.
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San.
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
COMPUTER PROGRAMMING I 5.04 Apply Decision Making Structures.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Controlling Program Flow with Decision Structures.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Microsoft Visual Basic 2012 CHAPTER FIVE Decision Structures.
Copyright © Don Kussee 1410-Ch5 #1031 CNS 1120 Chapter 5 Selection statements 1120-Ch5.PPT.
Chapter Five More on the Selection Structure Programming with Microsoft Visual Basic th Edition.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
More Visual Basic Code: if-then-else, for loops Controls: Multiple forms, List Boxes, Radio buttons, frames,
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
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”
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
Chapter 4: Decisions and Conditions
Visual Basic Fundamental Concepts
Chapter One: An Introduction to Programming and Visual Basic
5.04 Apply Decision Making Structures
A variable is a name for a value stored in memory.
Chapter 4: Decisions and Conditions
Chapter 4 The If…Then Statement
Visual Basic I Programming
CHAPTER FIVE Decision Structures.
CHAPTER FIVE Decision Structures.
Visual Basic..
Department Array in Visual Basic
Chapter 3: Introduction to Problem Solving and Control Statements
Objectives After studying this chapter, you should be able to:
Microsoft Visual Basic 2005: Reloaded Second Edition
Introduction to Computer Programming IT-104
Presentation transcript:

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 1 Chapter 4: The Selection Process in Visual Basic

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 2 Learning Objectives Understand the importance of the selection process in programming. Describe the various types of decisions that can be made in a computer program. Discuss the statements used in Visual Basic to make decisions. Understand the various comparison operators used in implementing decisions in Visual Basic. Use the If-Then-Else, If-Then-ElseIf, and Case decision structures. Use the list box control to select from a list of alternatives. Work with complex comparison structures and nested decisions to handle more sophisticated selection processes. Use the scroll bar to input integer values. Use the Form_Load event to execute a procedure when a form is loaded. Work with the debugging toolbar to find program errors.

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 3 The Selection Process One of the key operations of a computer is to select between two or more alternatives to make a decision. Every decision involves a comparison between a variable and a constant, variable, or expression using logical operators. Decisions can involve two-alternatives or multiple alternatives.

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 4 The If-Then-Else Decision Structure For two alternative decisions, the If-Then- Else decision structure should be used In pseudocode, this is: If condition is true then implement true alternative Else implement false alternative End Decision

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 5 Multiple Alternatives For multiple alternatives, the general form in pseudocode is: Select one: Condition 1 is true; implement alternative 1 Condition 2 is true: implement alternative 2 Condition 3 is true; implement alternative 3 End Selection.

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 6 The Two Alternative Decision Structure The If-Then-Else statement is: If condition is true Then statements for true alternative Else statements for false alternative End if The If-Then condition test expression1 comparison operator test expression2 where comparison operator is one of these six operators: Equal to: = Not equal to: <> Less then: < Greater than: > Less than or equal to: <= Greater than or equal to: >=

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 7 The If-Then-Else Decision Structure (cont.)

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 8 Example of If-Then-Else Decision to Compute Payroll sngPayRate = CCur(txtPayRate.text) intHours = CSng(txtHours.text) If intHours >= 40 Then curPay = sngPayRate * * _ sngPayRate * (intHours - 40) Else curPay = intHours * sngPayRate Endif txtPay.Text = Format(curPay, _ ”currency”)

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 9 One-Alternative Decision If there is only a true alternative, then this is a special case of the two-alternative decision structure If condition is true Then true alternative is implemented End If One-alternative decision can be combined with InputBox used to validate user input, e.g., to test that Customer Name textbox has something in it: If txtCustName.Text = “” then txtCustName.Text = InputBox(“Enter _ Name and try again”) Exit Sub End If Where the Exit Sub statement exits the event procedure

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 10 If-Then-ElseIf Decision Structure One way to implement a multiple alter- native decision structure is through the If- Then-ElseIf decision structure: If condition1 true Then first set of statements ElseIf condition2 true Then second set of statements ElseIf condition3 true Then third set of statements Else last set of statements End If

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 11 If-Then-ElseIf Decision Structure (Assume condition3 is True)

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 12 Example of If-Then-ElseIf for Letter Grade Determination Dim intAverage as Integer Dim strLetterGrade as String intAverage = CInt(txtAverage.Text) If intAverage >= 90 then strLetterGrade = “A” ElseIf Average >= 80 then strLetterGrade = “B” ElseIf Average >= 70 then strLetterGrade = “C” ElseIf Average >= 60 then strLetterGrade = “D” Else strLetterGrade = “F” End if. txtLetter.Text = strLetterGrade

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 13 Using the Select Case Decision Structure for Multiple Alternatives General form: Select Case expression Case Condition1 is true First set of statements Case Condition2 is true Second set of statements Case Condition3 is true Third set of statements Case Else Last set of statements End Select

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 14 Case Conditions Conditions for Case statement can be in 3 forms: Test ConditionExample Value or expressionCase 91, 92, 93 Range of valuesCase 90 to 100 Comparison conditionCase Is > 89

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 15 Example of Select Case to Determine LetterGrade Dim intAverage as Integer Dim strLetterGrade as String intAverage = CInt(txtAverage.Text) Select Case intAverage Case Is >= 90 strLetterGrade = “A” Case Is >= 80 strLetterGrade = “B” Case Is >= 70 strLetterGrade = “C” Case Is >= 60 strLetterGrade = “D” Case Else strLetterGrade = “F” End Select

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 16 Using the List Box The List box enables the user to select from a list of items. lst is the prefix for name and the List property of the list box can be set at design time or run time. The Text property of the list box is equal to the selected item. We can test the Text property to determine which item was selected.

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 17 More Complex Decisions Decisions within decisions are know as nested decisions Interior decision must be an alternative of outer decision Decisions that combine two or more test conditions using logical operators are known as compound decisions And, Or, Not, and Xor are logical operators Both conditions must be able to stand alone

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 18 Example of Nested Decisions Need to check if employee is hourly before checking for overtime: If strPayType = “Hourly” then If intHours > 40 Then curPay = 40 * sngPayRate _ (intHours-40) * sngPayRate Else curPay = intHours * sngPayRate End If Else curPay = 40 * sngPayRate End If

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 19 Example of Compound Decisions Using compound condition to test for average AND number of absences If sngAverage >= 90 AND intAbsences < 3 then strLetterGrade = “A” ElseIf sngAverage >= 80 AND intAbsences < 5 then strLetterGrade = “B” etc. In this case, if a student has an average of 93 and 4 absences, he/she will receive a grade of “B” because he/she has more than 3 absences.

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 20 Example of Using List Box Dim strVideoType as String Dim curPrice as Currency strVideoType = lstTypes.Text Select Case strVideoType Case “Kids” curPrice = 0.99 Case “Regular” curPrice = 1.99 Case “Classic” curPrice = 2.99 End Select txtVideoType.Text = Format(curPrice, _ ”currency”) End Sub

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 21 Using the Form_Load Event The Form_Load event occurs when the project is started and the form is loaded. Code from a command button can be cut (or copied) and then pasted into the form_load event

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 22 Using the Debug ToolBar To view the Debug Toolbar, Select View|Toolbars and click the Debug checkbox The Debug Toolbar Run Stop Step Into Step Out Immediate Window Quick Watch Break Toggle Step Over Locals Watch Window Call Stack Breakpoint Window

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 23 Debugging With the Immediate Window By clicking the Immediate Window icon on the debug toolbar, you can then print the current value of a textbox or variable Use the Print variable or textbox command to display its current value This can enable you to find errors in the code