VB Script – A Widely Used
VB Script – A Widely Used
Scripting Language
What we learn last session?
What we learn last session?
Branching
Branching
Branching using If
Branching using If
…
…
Then
Then
…
…
Else
Else
statement.
statement.
Branching using Select Case
Branching using Select Case
Subjects for session 5
Subjects for session 5
Looping through code.
Looping through code.
Do
Do
…
…
Loop
Loop
While
While
…
…
Wend
Wend
For
For
…
…
e!t
e!t
For
For
…
…
Each
Each
E!it
Looping hrough !ode
Looping hrough !ode
Looping allo#s $ou to run a group of statements Looping allo#s $ou to run a group of statements repeatedl$.
repeatedl$.
Some loops repeat statements until a condition is Some loops repeat statements until a condition is False
False%%
others repeat statements until a condition is others repeat statements until a condition is True
True..
There are also loops that repeat statements a There are also loops that repeat statements a specific num&er of times.
"o While#Until
Looping hrough !ode
Looping hrough !ode
Using "o$Loop State%ent
Using "o$Loop State%ent
)epeats a &loc* of statements #hile a condition is
)epeats a &loc* of statements #hile a condition is TrueTrue or until a or until a condition &ecomes
condition &ecomes TrueTrue..
The follo#ing looping statements are a+aila&le in The follo#ing looping statements are a+aila&le in
,BScript-Do
Do //WhileWhile 00 UntilUntil11 conditioncondition
statementsstatements ExitExit DoDo statementsstatements Loop Loop Do Do statementsstatements ExitExit DoDo statementsstatements Loop
Looping hrough !ode
Looping hrough !ode
Using "o$Loop State%ent
Using "o$Loop State%ent
4ou can e!it a
4ou can e!it a Do...LoopDo...Loop &$ using the &$ using the Exit DoExit Do statement. statement. Because $ou usuall$ #ant to e!it onl$ in certain situations5 Because $ou usuall$ #ant to e!it onl$ in certain situations5 such as to a+oid an endless loop5 $ou should use the
such as to a+oid an endless loop5 $ou should use the ExitExit Do
Do statement in the statement in the TrueTrue statement &loc* of an statement &loc* of an If
If ...ThenThen...ElseElse statement. statement. If the condition is
Looping hrough !ode
Looping hrough !ode
Using "o$Loop State%ent
Using "o$Loop State%ent
Dim
Dim &Chec*5 Count &Chec*5 Count &Chec* 7
&Chec* 7 TrueTrue - iCount 7 8- iCount 7 8 9 Initiali:e +aria&les.9 Initiali:e +aria&les.
Do
Do 9 ;uter loop.9 ;uter loop.
Do
Do WhileWhile iCount < 28 iCount < 28 9 Inner loop9 Inner loop.. iCount 7 iCount = >
iCount 7 iCount = > 9 Increment Counter.9 Increment Counter.
If
If iCount 7 >8 iCount 7 >8 ThenThen 9 If condition is True...9 If condition is True...
&Chec* 7
&Chec* 7 FalseFalse 9 set +alue of flag to False.9 set +alue of flag to False.
Exit
Exit DoDo 9 E!it inner loop.9 E!it inner loop.
End
End If If
Loop
Loop
Loop
Looping hrough !ode
Looping hrough !ode
Using While$Wend State%ent
Using While$Wend State%ent
E!ecutes a series of statements as long as a gi+en condition E!ecutes a series of statements as long as a gi+en condition is
is TrueTrue.. The
The WhileWhile...WendWend statement is pro+ided in ,BScript for statement is pro+ided in ,BScript for those #ho are familiar #ith its usage.
those #ho are familiar #ith its usage.
@o#e+er5 &ecause of the lac* of fle!i&ilit$ in
@o#e+er5 &ecause of the lac* of fle!i&ilit$ in WhileWhile...WendWend55 it is recommended that $ou use
it is recommended that $ou use DoDo...LoopLoop instead. instead. The
The WhileWhile...WendWend statement is pro+ided in ,BScript for statement is pro+ided in ,BScript for those #ho are familiar #ith its usage.
Looping hrough !ode
Looping hrough !ode
Using &or$'e(t State%ent
Using &or$'e(t State%ent
)epeats a group of statements a specified num&er of times. )epeats a group of statements a specified num&er of times. 4ou can use
4ou can use ForFor...NextNext statements to run a &loc* of statements to run a &loc* of statements a specific num&er of times.
statements a specific num&er of times.
For loops5 use a counter +aria&le #hose +alue increases or For loops5 use a counter +aria&le #hose +alue increases or decreases #ith each repetition of the loop
decreases #ith each repetition of the loop
Dim Dim ! ! For For ! 7 > To '8 ! 7 > To '8 A$roc A$roc Next Next
Looping hrough !ode
Looping hrough !ode
Using &or$'e(t State%ent
Using &or$'e(t State%ent
sing the
sing the StepStep *e$#ord5 $ou can increase or decrease the *e$#ord5 $ou can increase or decrease the counter +aria&le &$ the +alue $ou specif$.
counter +aria&le &$ the +alue $ou specif$.
In the follo#ing e!ample5 the counter +aria&le is In the follo#ing e!ample5 the counter +aria&le is incremented &$ 2 each time the loop repeats.
incremented &$ 2 each time the loop repeats.
When the loop is finished5 the total is the sum of 25 "5 (5 65 When the loop is finished5 the total is the sum of 25 "5 (5 65 and >8.
and >8.
Dim
Dim 5 itotal 5 itotal For
For 7 2 7 2 ToTo >8 >8 StepStep 2 2 itotal 7 itotal =
itotal 7 itotal = Next
Next
Asg 7 The total is
Looping hrough !ode
Looping hrough !ode
Using &or$'e(t State%ent
Using &or$'e(t State%ent
4ou can e!it an$
4ou can e!it an$ For...NextFor...Next statement &efore the counter statement &efore the counter reaches its end +alue &$ using the
reaches its end +alue &$ using the ExitExit ForFor statement. statement. Because $ou usuall$ #ant to e!it onl$ in certain
Because $ou usuall$ #ant to e!it onl$ in certain
situations5 such as #hen an error occurs5 $ou should use situations5 such as #hen an error occurs5 $ou should use the
the ExitExit ForFor statement in the statement in the TrueTrue statement &loc* of an statement &loc* of an If...Then...Else
If...Then...Else statement. statement. If the condition is
G
G For Each...NextFor Each...Next loop is similar to a loop is similar to a For...NextFor...Next loop.
loop.
Instead of repeating the statements a specified Instead of repeating the statements a specified num&er of times5 a
num&er of times5 a ForFor Each...NextEach...Next loop repeats a loop repeats a group of statements for each item in a collection of group of statements for each item in a collection of o&ects or for each element of an arra$.
o&ects or for each element of an arra$.
This is especiall$ helpful if $ou don9t *no# ho# This is especiall$ helpful if $ou don9t *no# ho# man$ elements are in a collection.
man$ elements are in a collection.
Looping hrough !ode
Looping hrough !ode
Using &or )ach***'e(t State%ent
Using &or )ach***'e(t State%ent
Looping hrough !ode )(it State%ent
Looping hrough !ode )(it State%ent
E!its a &loc* of
E!its a &loc* of Do...LoopDo...Loop55 For...NextFor...Next55 FunctionFunction5 or5 or SubSub code.
code.
Exit Do Exit Do
ro+ides a #a$ to e!it a
ro+ides a #a$ to e!it a Do...LoopDo...Loop statement. statement. It can &e used onl$ inside a
It can &e used onl$ inside a Do...LoopDo...Loop statement. statement. Exit For
Exit For
ro+ides a #a$ to e!it a
ro+ides a #a$ to e!it a ForFor loop. loop. It can &e used onl$ in a
It can &e used onl$ in a For...NextFor...Next or or ForFor Each...NextEach...Next loop. loop. Exit Function
Exit Function
Immediatel$ e!its the
Immediatel$ e!its the FunctionFunction procedure in #hich it appears. procedure in #hich it appears. Exit ropert!
Exit ropert!
Immediatel$ e!its the
Immediatel$ e!its the ropert!ropert! procedure in #hich it appears. procedure in #hich it appears. Exit Sub
Exit Sub
Immediatel$ e!its the