Chapter 4: The Selection Process in Visual Basic
description
Transcript of Chapter 4: The Selection Process in Visual Basic
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
1Copyright © 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
Chapter 4: The Selection Process in
Visual Basic
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
2Copyright © 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
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.
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
3Copyright © 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
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.
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
4Copyright © 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
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
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
5Copyright © 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
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.
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
6Copyright © 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
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 conditiontest 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: >=
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
7Copyright © 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
The If-Then-Else Decision Structure (cont.)
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
8Copyright © 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
Example of If-Then-Else Decision to Compute Payroll
sngPayRate = CCur(txtPayRate.text)intHours = CSng(txtHours.text)If intHours >= 40 Then curPay = sngPayRate * 40 + 1.5 * _ sngPayRate * (intHours - 40)Else curPay = intHours * sngPayRate EndiftxtPay.Text = Format(curPay, _ ”currency”)
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
9Copyright © 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
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
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
10Copyright © 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
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
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
11Copyright © 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
If-Then-ElseIf Decision Structure(Assume condition3 is True)
If condition1 true thenfirst set of statements
else if condition2 true thensecond set of statements
else if condition3 true thenthird set of statements
elsefourth set of statements
end if
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
12Copyright © 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
Example of If-Then-ElseIf for Letter Grade Determination
Dim intAverage as IntegerDim strLetterGrade as StringintAverage = 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
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
13Copyright © 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
Using the Select Case Decision Structure for Multiple Alternatives
• General form:Select Case expressionCase Condition1 is true
First set of statementsCase Condition2 is true
Second set of statementsCase Condition3 is true
Third set of statementsCase Else
Last set of statementsEnd Select
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
14Copyright © 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
Case Conditions
• Conditions for Case statement can be in 3 forms:
Test Condition Example
Value or expression Case 91, 92, 93
Range of values Case 90 to 100
Comparison condition Case Is > 89
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
15Copyright © 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
Example of Select Case to Determine LetterGrade
Dim intAverage as IntegerDim strLetterGrade as StringintAverage = 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
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
16Copyright © 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
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.
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
17Copyright © 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
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
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
18Copyright © 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
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 + 1.5 + _
(intHours-40) * sngPayRate
Else
curPay = intHours * sngPayRate
End If
Else
curPay = 40 * sngPayRate
End If
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
19Copyright © 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
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.
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
20Copyright © 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
Example of Using List BoxDim strVideoType as StringDim curPrice as CurrencystrVideoType = 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
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
21Copyright © 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
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
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
22Copyright © 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
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
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
23Copyright © 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
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