VB Script (Beginners) (II)
Conditional ConstructsIn VBScript we have four conditional statements:
if statement - use this statement if you want to execute a set of code when a condition is true
if...then...else statement - use this statement if you want to select one of two sets of lines to execute
if...then...elseif statement - use this statement if you want to select one of many sets of lines to execute select case statement - use this statement if you want to select one of many sets of lines to execute If....Then.....Else You should use the If...Then...Else statement if you want to execute some code if a condition is trueFor example: if i=10 Then msgbox "Hello"
If you want to execute more than one statement when a condition is true, you must put each statement on separate lines and end the statement with the keyword "End If"
For Example:
If i = 10 Then
msgbox “POOJA”
i = i+1
end If
(Here you are telling the code to perform multiple actions if the condition is true)
To execute a statement if a condition is true and execute another statement if the condition is not true, you must add the "Else" keyword.
If i = 10 Then
msgbox “Hello Pooja”
else
msgbox “Goodbye Pooja”
end If
If....Then.....ElseifYou can use the if...then...elseif statement if you want to select one of many blocks of code to execute:
if payment="Electricity Bill" then
msgbox "You are going to pay Electricity Bill!"
elseif payment="Telephone bill" then
msgbox "You are going to pay Telephone Bill।"
elseif payment="Gas Bill" then
msgbox "You are going to pay Gas Bill।"
Else
msgbox "Other Bill Option।"
end If
Select Case
You can also use the SELECT statement if you want to select one of many blocks of code to execute:
select case Bill
case "Electricity Bill"
msgbox "You are going to pay Electricity Bill"
case "Telephone Bill"
msgbox "You are going to pay Telephone Bill"
case "Gas Bill"
msgbox "You are going to pay Gas Bill"
case Else
msgbox "Other Bill Options"
end select Looping Statements
In VBScript we have four looping statements: For...Next statement - runs statements a specified number of times.
For Each...Next statement - runs statements for each item in a collection or each element of an array Do...Loop statement - loops while or until a condition is true While...Wend statement - Do not use it - use the Do...Loop statement instead For...Next statementFor i=1 to 10
some code
Next
The For statement specifies the counter variable (i) and its start and end values। The Next statement increases the counter variable (i) by one.
Step Keyword Using the Step keyword, you can increase or decrease the counter variable by the value you specify.
For i=2 To 10 Step 2
some code
Next
To decrease the counter variable, you must use a negative Step value
For i=10 To 2 Step -2
some code
Next
For Each...Next Loop
A For Each...Next loop repeats a block of code for each item in a collection, or for each element of an array.
dim cars(2)
cars(0)="Volvo"
cars(1)="Saab"
cars(2)="BMW"
For Each x in cars document.write(x & " ")
Next
Do...Loop
You can use Do...Loop statements to run a block of code when you do not know how many repetitions you want.
Repeating Code While a Condition is True
You use the While keyword to check a condition in a Do...Loop statement.Do While i>10
some code ----> If i equals 9, the code inside the
loop loop above will never be executed.
Notice the difference between the two examples.
Do
some code ----> Code inside the loop will be executed Loop While i>10 at least one time even if i is lessthan 10.
Exit a Do...Loop
Do Until i=10
i=i-1
If i<10>Then Exit Do
Loop
EX: dim names(2)
arrays are of two types à Local Arrays And Global Arrays
=> global array is an array that can be used by all functions and procedures. It is declared at the beginning of the VBScript Code.
TheDim statement is used to declare arrays।
The syntax for declaring an array is as follows:
Dim ArrayName(subscriptvalue)
=>ArrayName is the unique name for the array =>SubscriptValue is a numeric value that indicates the number of elements in the array dimension within the array Example:
Note:The No_Passengers can store 4 values। Assigning values to the array
No_Passengers(0) = 1No_Passengers(1) = 2
No_Passengers(2) = 3
No_Passengers(3) = 4
Static and Dynamic Arrays:Static Array--- The size of a static array cannot be altered at run time.
Dynamic Array --- A dynamic array can be resized at any time. The array size can be changed at run time.
No comments:
Post a Comment