The Selection Process in VBScript - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

The Selection Process in VBScript

Description:

You would make it as a UGA MBA' Case 'B' Comment = 'Good work! Think about Auburn for ... if PayType = Hourly then Hourly Can make overtime. If hours 40 then ... – PowerPoint PPT presentation

Number of Views:119
Avg rating:3.0/5.0
Slides: 18
Provided by: patmc2
Category:

less

Transcript and Presenter's Notes

Title: The Selection Process in VBScript


1
The Selection Process in VBScript
2
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

3
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

4
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.

5
The 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

6
The If-Then-Else Decision Structure (cont.)
7
Example 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
8
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
  • 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.

9
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

10
If-Then-ElseIf Decision StructureAssume
condition3 is True
11
Example 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.

12
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

13
Case 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

14
Example 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
15
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

16
Example 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

17
Example 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.
Write a Comment
User Comments (0)
About PowerShow.com