What is VBScript?
- Based on the Visual Basic family of languages
- Supports object oriented features
- Interpreted
- Loosely Typed
- VBScript is not case sensitive
- Each statement can only exist on one line
- Statements can be either simple statements or compound statements
- Must begin with a letter or the underscore
- Good: count, strInfo, _data
- Bad: 2day, 4get, *count violate this rule.
- Cannot contain special characters except the underscore
- Good: Names using alphabets, numbers and the underscore.
- Bad: Names using symbols and other special characters.
- Cannot match a reserved word
- Bad: for, if, Select
- Must be unique within a context
- Must be less than 256 characters
objDescriptiveName
obj
Three letter prefix for the data type
DescriptiveName
- A descriptive name for the variable
- First letter of each word in upper case
Numeric
- 1
- 3.14
- 3E7
Boolean
- True
- False
Characters
- Hello World
- 123 Straw Lane
- 43210
- Date
#07/21/2006#
Variables
Dim variableName
- Variables hold values that can change during program execution
- They function just like temporary storage locations
Const constantName = value
-
Constant names hold values that cannot change during program execution
-
Constants should be used in place of hard-coded values
- Operations are the many forms of computations, comparisons etc that can be performed in VB
- Most operations are binary in the form:
- operand1 operator operand2
- Other operations are unary in the form
- operator operand1
- When an operator has 2 symbols, you cannot separate them with a space:
- Good: 2 <> 3
- Bad: 2 < > 3
- Used to perform calculations
- Both operands must be numeric values
- The result is a numeric value
- Subtraction, Negation
* Multiplication
/ Division
^ Exponent
Mod Modulo
Comparison:
- Used to compare the value of two items
- Both operands must be of the same data type
-
The result is a Boolean value
|
= |
Equality |
|
<> |
Inequality |
|
< |
Less than |
|
> |
Greater than |
|
<= |
Less than or equals |
|
>= |
Greater than or equals |
Logical
- Used to reduce Boolean values into a single Boolean value
- Both operands must be Boolean values
- The result is a Boolean value
|
And |
Conjunction |
|
Or |
Disjunction |
|
Not |
Negation |
And Truth Table:
- Conjunction
- Used when both conditions are required
|
Operand1 |
Operand2 |
Result |
|
T |
T |
T |
|
T |
F |
F |
|
F |
T |
F |
|
F |
F |
F |
Or Truth Table
- Disjunction
- Used when either condition is sufficient
|
Operand1 |
Operand2 |
Result |
|
T |
T |
T |
|
T |
F |
T |
|
F |
T |
T |
|
F |
F |
F |
Not Truth Table
- Unary Operand
- Used to change the value of a condition
|
Operand 1 |
Result |
|
T |
F |
|
F |
T |
- Changes the value of a variable
- =
- Uses the same symbol as an equality check operator.
- Assignment only occurs when used in any of the following forms:
- variable = literal value
- variable = expression
- variable = variable
- Combines 2 data types for display as a string
- &
- Allows you to avoid running certain sections of your code
- Code is only executed when a condition (or conditions) evaluate to True
- Provides a single application the ability to react differently to different input values
If condition Then
statement(s)
End If
-
Performs an operation only if the condition evaluates to True
-
Used when an action is either performed or not performed.
If condition Then
statement(s)
Else
statement(s)
End If
- Performs the If portion only if the condition evaluates to True and the Else portion otherwise.
- Used in an either or scenario when an operation is always performed.
If condition1 Then
statement(s)
ElseIf condition2 Then
statement(s)
Else
statement(s)
End If
- Only one section can be executed even if many conditions evaluate to True.
- Used in a multiple choice scenario
- Inclusion of the last Else is optional
- Allows you to repeat running certain sections of your code
- Code executes when a condition (or conditions) evaluate to True
- Be careful with Loops. They can result in infinite processing.
- Forms
- Entry Condition
- Entry only when a initial condition is met
- Iterated
- Repeats for a specific number of times
If no, then you dont need the loop
Can this loop ever be exited
If no, then you have an infinite loop
While
While condition
statement(s)
Wend
- Entry Condition Loop
- Simplest form of the loop
- Requires manual modification of the loop condition
For variable = start To finish [Step change]
statement(s)
Next
- Iterated Loop
- Favored because all the loop details are in the definition statement
Examples:
Declaring Variables:
dim name
name=some value
Assigning Values to Variables
name="Hege"
i=200
If....Then.....Else
if i=10 Then
msgbox "Hello"
i = i+1
end If
if i=10 then
msgbox "Hello"
else
msgbox "Goodbye"
end If
if payment="Cash" then
msgbox "You are going to pay cash!"
elseif payment="Visa" then
msgbox "You are going to pay with visa."
elseif payment="AmEx" then
msgbox "You are going to pay with American Express."
else
msgbox "Unknown method of payment."
end If
| < Prev |
|---|



