Title: Intro to VBScript
1Intro to VBScript
2Conditional Branching
An application is to compute the amount due for
a sale by multiplying the selling price of a
widget times the number of widgets sold.
However, a 10 discount is allowed if the
customer presents a sale coupon. How can we
program this task when the discount should only
be taken in some cases?
3Charting Program Flow
Get WidgetPrice
Get QuantitySold
Get SaleCoupon (yes or no)
AmountDue WidgetPrice x QuantitySold
SaleCoupon 'yes'
yes
AmountDue AmountDue 90
no
Display AmountDue
4The VBScript implementation
- '
- ' SalesAmount.vbs
- '
- option explicit
- dim WidgetPrice, QuantitySold, SaleCoupon,
AmountDue - WidgetPrice InputBox("Please enter widget
price ") - QuantitySold InputBox("Please enter quantity
sold ") - SaleCoupon InputBox("Sale Coupon Presented
(yes/no)? ") - AmountDue WidgetPrice QuantitySold
- if SaleCoupon "yes" then
- AmountDue AmountDue .90
- end if
- Wscript.echo("The amount due is " AmountDue)
5The basic if statement
- Syntax of the basic "if" statement in VBScript
- if ltboolean conditiongt then
- program statement
- program statement
- .
- end if
- The "end if" line indicates the end of the set of
conditional program statements that should be
executed if the boolean condition is true. After
the conditional programming statements have been
executed the program will continue to execute
with the next line after the "end if" - If the boolean condition is false the
conditional statements would not be executed.
Instead the program would continue to execute
beginning with the first line of code after the
"end if". - The indention of the conditional processing
statements is not required by the language syntax
but is an essential part of making a program easy
to read and understand.
6The if-then-else statement
- Another form of if statement is the if-then-else
statement - if ltboolean conditiongt then
- program statement
- program statement
- .
- else
- program statement
- program statement
- .
- end if
- The conditional statements between the "else" and
the "end if" are only executed if the boolean
condition is false. An example is presented on
the next slide.
7If-then-else example
- '
- ' Raise.vbs
- ' Gives 5 raise and 1 bonus if a male
- ' and 7 raise and a 2 bonus if a female
- '
- option explicit
- dim CurrentSalary, Gender, Raise, NewSalary,
Bonus - CurrentSalary InputBox("Please enter the
current salary ") - Gender InputBox("Please enter the gender (M/F)
") - if Gender "F" then
- Bonus CurrentSalary .02
- Raise CurrentSalary .07
- else
- Bonus CurrentSalary .01
- Raise CurrentSalary .05
- end if
- NewSalary CurrentSalary Raise
- Wscript.echo("The Xmas bonus is " Bonus)
- Wscript.echo("The new salary is " NewSalary)
8If-then-else statement in Java
- In some languages like JAVA and C the set of
conditional statements is designated with braces
instead of an "end if" statement. - if (gender.equals("F"))
-
- bonus currentSalary .02
- raise currentSalary .07
-
- else
-
- bonus currentSalary .01
- raise currentSalary .05
-
-
9Compound If statements
- The boolean condition in an if statement can be
compound. For example, suppose our salary
student only showed discrimination in the
accounting department such that only females in
the accounting department should get the extra
raise. - if Gender "M" and Department "accounting"
then - Bonus CurrentSalary .02
- Raise CurrentSalary .07
- else
- Bonus CurrentSalary .01
- Raise CurrentSalary .05
- end if
10Nested If statements
- One if statement can be contained within another
to express more complex logic. For example,
consider the following plan for granting raises
All employees should receive a 2 cost of living
raise. In addition all females should receive an
additional 1. Finally, employees in the IT
department should receive an additional 3. - if Gender "F" then
- if Department "IT" then
- Raise CurrentSalary (.02 .01 .03)
- else
- Raise CurrentSalary (.02 .01)
- end if
- else
- if Department "IT" then
- Raise CurrentSalary (.02 .03)
- else
- Raise CurrentSalary .02
- end if
- end if
11Another way
- There are almost always several ways to represent
the same logic. An inefficient way to implement
the raise plan from the last slide is - if Gender "F" and Department "IT" then
- Raise CurrentSalary (.02 .01 .03)
- end if
- if Gender "F" and Deparment ltgt "IT" then
- Raise CurrentSalary (.02 .01)
- end if
- if Gender "M" and Department "IT" then
- Raise CurrentSalary (.02 .03)
- end if
- if Gender "M" and Department ltgt "IT" then
- Raise CurrentSalary .02
- end if
12Yet another way
- There are almost always several ways to represent
the same logic. A very efficient way to
implement the raise plan from the last two slides
is - RaisePercent .02 'the COLA raise for
everyone - if Gender "F" then
- RaisePercent RaisePercent .01
- end if
- if Department "IT" then
- RaisePercent RaisePercent .03
- end if
- Raise CurrentSalary RaisePercent
- When choosing between algorithms consider which
are most efficient but also which are easy to
understand.
13Linear nested ifs
- Nested if statements used to select one value
from a mutually exclusive set of values is called
a linear nested if. Consider the following logic
that determines which of four seasons it is. - if Season "Fall" then
- wscript.echo("Time for football")
- else
- if Season "Winter" then
- wscript.echo("Time for basketball")
- else
- if Season "Spring" then
- wscript.echo("Time for baseball")
- else
- if Season "Summer" then
- wscript.echo("Time for vacation")
- end if
- end if
- end if
- end if
14The elseif statement
- Some languages, including VBScript, implement an
elseif (or elsif) statement in order to present
the logic of a linear nested if more clearly and
concisely - if Season "Fall" then
- wscript.echo("Time for football")
- elseif Season "Winter" then
- wscript.echo("Time for basketball")
- elseif Season "Spring" then
- wscript.echo("Time for baseball")
- elseif Season "Summer" then
- wscript.echo("Time for vacation")
- end if
- The above is considered a single if statement
with a single "end if" instead of four nested if
statements.
15The select case statement
- Some languages, including VBScript, implement an
even more restrictive structure called a SELECT,
CASE, or SWITCH statement. In these cases the
variable being determined is specified at the top
of the statement - Select Case Season
- Case "Fall"
- wscript.echo("Time for football")
- Case "Winter"
- wscript.echo("Time for basketball")
- Case "Spring"
- wscript.echo("Time for baseball")
- Case "Summer"
- wscript.echo("Time for vacation")
- Case Else
- wscript.echo("Invalid season entered")
- End Select
16A word on error trapping
- As a general rule you should get in the habit of
trapping and handling erroneous data that might
be entered or conditions that might occur. On
the previous slide this was done with the CASE
ELSE statement. Below it is done with a final
ELSE - if Season "Fall" then
- wscript.echo("Time for football")
- elseif Season "Winter" then
- wscript.echo("Time for basketball")
- elseif Season "Spring" then
- wscript.echo("Time for baseball")
- elseif Season "Summer" then
- wscript.echo("Time for vacation")
- else
- wscript.echo("An invalid season was
entered") - end if
17In-lab requirement
- Create a folder named LAB3 in your itec110
student folder. - Create the two VBScript programs described in the
next two slides and make sure they reside in your
lab3 folder. - When writing the programs make sure to use
thoughtful variable names - Include your name, the program name, and the date
at the top of each program in a tombstone.
18In-lab program 1 discounts.vbs
- Create a program which accepts a quantity of
widgets and also a widget price. - Have your program output the amount due assuming
the following - If less than 10 widgets are sold there is no
discount - If between 10 and 99 widgets are sold there is a
5 discount. - If 100 or more widgets are sold there is a 10
discount.
19In-lab program 2 epoch.vbs
- One solution to the YTK problem was to adjust
programs to make reasonable assumptions about
what four digit year is intended when only two
digits are entered. This usually involves a
cut-off year called the epoch. - Create a program that accept a year between 0 and
99. - Generate an error message if year is over 99.
- Generate an error message if year is under 0.
- If year is less than 50 assume the person means
21st century and display the four digit date. - If year is greater than or equal to 50 assume
person means 20th century and display the four
digit date. - For example, if someone enters 98 the output of
your program should be 1998. If someone enters 6
the output should be 2006. You can use addition
or concatenation to adjust the date.
20Homework Assignment
- Write a program named estimate.vbs that follows
these rules for estimating the cost of a house. - The base cost is 50 per square foot.
- The base cost can very depending on how fancy the
house is. Here are some adjustments to make. - A single story house costs an additional 10 per
square foot to build (2 story houses are cheaper) - Add 10,000 to the total cost if there is a
garage. - Add 2 per square foot if there are hardwood
floors. - Add 5,000 to the total cost if there are custom
kitchen cabinets. - Add 10 per square foot if the house has a
basement instead of a crawl space. - Add 3,000 for each fireplace.
- Your program should prompt for the necessary
inputs and output a total cost. - Place your estimate.vbs program in your lab3
folder no later than the beginning of the next
lab period.
21Homework Challengefor those who have programmed
before
- Write a program named CheckAmount.vbs that
accepts as input a number between 0.01 and
999999.99. - Generate an error message if the number entered
is out of range - Your program should convert the number to a
written amount like would be on a check. For
example - Input 456.12
- Output "Four-hundred-fifty-six and 12/100
dollars" - Use arrays if you want to.
- If you want to, have your program call a function
that actually does the conversion. That would
make the algorithm more portable.