Title: The Selection Process in VBScript
1The Selection Process in VBScript
2The 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
3The 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
4Multiple 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.
5The Two Alternative Decision Structure
- The If-Then-Else statement is
- lt If condition is true Then
- statements for true alternative
- Else
- statements for false alternative
- End if gt
- The If-Then condition
- test expression1 comparison operator test
expression2 - where comparison operator is one of these six
operators - Equal to Less then lt
- Greater than gt Less than or equal to lt
- Greater than or equal to gt Not equal to ltgt
6The If-Then-Else Decision Structure (cont.)
7Example of If-Then-Else Decision to Compute
Payroll
lt Option Explicit gt ltHTMLgt lt Dim Payrate,
Hours, GrossPay, PayType, Netpay Payrate
10.50 Hours 50 If Hours gt 40 then GrossPay
PayRate 40 1.5 PayRate (Hours -
40) Else GrossPay Hours PayRate End if
gt The pay is ltFormatCurrency(Pay)gtltbrgt lt/HTMLgt
Save as Payroll.asp and add the line lta href
Payroll.aspgtCalculate Gross Paylt/agt to
default.asp and test the application
8One-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
- Example
- Add the following lines to PayCalc.asp prior to
lt/HTMLgt - Paytype Parttime
- If Paytype Parttime then
- NetPay GrossPay - .0745GrossPay Deduct SS
- End if gt
- Net pay is ltformatcurrency(Netpay)gt
- Resave Payroll.asp and run it.
9If-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
10If-Then-ElseIf Decision StructureAssume
condition3 is True
11Example of If-Then-ElseIf
- lt Option Explicitgt
- ltHTMLgt
- lt Dim Average, LetterGrade
- Average 75
- If Average gt 90 then
- LetterGrade A
- ElseIf Average gt 80 then
- LetterGrade B
- ElseIf Average gt 70 then
- LetterGrade C
- ElseIf Average gt 60 then
- LetterGrade D
- Else
- LetterGrade F
- End if gt
- The students letter grade is ltLetterGradegt
- lt/HTMLgt
- Save as gradecalc.asp and add a reference to it
in your default.asp page.
12Using 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
13Case Conditions
- Conditions for Case statement can be in 1 form as
a list of matching values, eg - Case 91, 92, 93
- Case A
- Case Blue
14Example of Select Case
lt option explicitgt lthtmlgt ltDim LetterGrade,
Comment Lettergrade "B" Select Case
LetterGrade Case "A" Comment "Excellent
work! You would make it as a UGA MBA" Case
"B" Comment "Good work! Think about Auburn for
your MBA" Case "C" Comment "Satisfactory.
Have you thought about Clemson?" Case
"D" Comment "Barely passing. You should think
about studying more!" Case Else Comment
"Failing work. The Univ of Florida is waiting for
you!" End Selectgt ltCommentgt lt/htmlgt Save
as MakeComment.asp and add a reference to it in
your default.asp page
15More 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
16Example of Nested Decisions
- Need to check if employee is hourly before
checking for overtime - if PayType Hourly then Hourly Can make
overtime - If hours gt 40 then
- Pay 40 PayRate 1.5 (Hours-40) PayRate
- Else
- Pay Hours PayRate
- End if
- Else Salaried employee cannot be paid overtime
- Pay 40 PayRate
- End if
17Example of Compound Decisions
- Using compound condition to test for average AND
number of absences - If Average gt 90 AND Absences lt 3 then
- LetterGrade A
- ElseIf Averager gt 80 AND Absences lt 5 then
- LetterGrade 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. -