8/10/2019 How to Do Programming in VBScript
1/18
1
Vb Script
http://www.apextgi.in/8/10/2019 How to Do Programming in VBScript
2/18
2
What we learn last session?
Branching
Branching using If Then Elsestatement.
Branching using Select Case
8/10/2019 How to Do Programming in VBScript
3/18
3
Subjects for session 5
Looping through code.Do Loop
While Wend
For
e!tFor Each
E!it statement.
8/10/2019 How to Do Programming in VBScript
4/18
"
Looping Through Code
Looping allo#s $ou to run a group of statementsrepeatedl$.
Some loops repeat statements until a condition isFalse %
others repeat statements until a condition isTrue .
There are also loops that repeat statements aspecific num&er of times.
8/10/2019 How to Do Programming in VBScript
5/18
'
Do While/Until
8/10/2019 How to Do Programming in VBScript
6/18
(
Looping Through Code
Using Do Loop State!ent)epeats a &loc* of statements #hile a condition is True or until acondition &ecomes True .
The follo#ing looping statements are a+aila&le in ,BScript-
Do / While 0 Until 1 conditionstatements
Exit Dostatements
Loop
Do statementsExit Dostatements
Loop / While 0 Until 1 condition
8/10/2019 How to Do Programming in VBScript
7/18
Looping Through Code
Using Do Loop State!ent4ou can e!it a Do...Loop &$ using the Exit Do statement.
Because $ou usuall$ #ant to e!it onl$ in certain situations5such as to a+oid an endless loop5 $ou should use the ExitDo
statement in theTrue
statement &loc* of anIf ... Then ... Else statement.
If the condition is False 5 the loop runs as usual.
8/10/2019 How to Do Programming in VBScript
8/18
6
Looping Through Code
Using Do Loop State!entDim &Chec*5 Count
&Chec* 7 True - iCount 7 8 9 Initiali:e +aria&les.
Do 9 ;uter loop.
Do While iCount < 28 9 Inner loop .iCount 7 iCount = > 9 Increment Counter.
If iCount 7 >8 Then 9 If condition is True...
&Chec* 7 False 9 set +alue of flag to False.
Exit Do 9 E!it inner loop.
End If
Loop
Loop Until &Chec* 7 False 9 E!it outer loop immediatel$.
8/10/2019 How to Do Programming in VBScript
9/18
?
Looping Through Code
Using While Wend State!entE!ecutes a series of statements as long as a gi+en conditionis True .
The While ... Wend statement is pro+ided in ,BScript forthose #ho are familiar #ith its usage.
@o#e+er5 &ecause of the lac* of fle!i&ilit$ in While ... Wend 5it is recommended that $ou use Do ... Loop instead.
The While ... Wend statement is pro+ided in ,BScript forthose #ho are familiar #ith its usage.
8/10/2019 How to Do Programming in VBScript
10/18
>8
Looping Through Code
Using "or #e$t State!ent)epeats a group of statements a specified num&er of times.
4ou can use For ... Next statements to run a &loc* ofstatements a specific num&er of times.
For loops5 use a counter +aria&le #hose +alue increases ordecreases #ith each repetition of the loop
Dim !For ! 7 > To '8
A$ rocNext
8/10/2019 How to Do Programming in VBScript
11/18
>>
Looping Through Code
Using "or #e$t State!entsing the Step *e$#ord5 $ou can increase or decrease the
counter +aria&le &$ the +alue $ou specif$.
In the follo#ing e!ample5 the counter +aria&le isincremented &$ 2 each time the loop repeats.
When the loop is finished5 the total is the sum of 25 "5 (5 65and >8.
Dim 5 itotalFor 7 2 To >8 Step 2
itotal 7 itotal = NextAsg 7 The total is itotal
8/10/2019 How to Do Programming in VBScript
12/18
>2
Looping Through Code
Using "or #e$t State!ent4ou can e!it an$ For...Next statement &efore the counterreaches its end +alue &$ using the Exit For statement.
Because $ou usuall$ #ant to e!it onl$ in certainsituations5 such as #hen an error occurs5 $ou should use
the Exit For statement in the True statement &loc* of anIf...Then...Else statement.
If the condition is False 5 the loop runs as usual.
8/10/2019 How to Do Programming in VBScript
13/18
>3
Looping Through Code
Using "or %ach&&e$t State!entG For Each...Next loop is similar to a For...Next loop.
Instead of repeating the statements a specified
num&er of times5 a For Each...Next loop repeats agroup of statements for each item in a collection ofo& ects or for each element of an arra$.
This is especiall$ helpful if $ou don9t *no# ho#
man$ elements are in a collection.
8/10/2019 How to Do Programming in VBScript
14/18
>"
Looping Through Code
%$it State!entE!its a &loc* of Do...Loop 5 For...Next 5 Function 5 or Sub code.
Exit Do ro+ides a #a$ to e!it a Do...Loop statement.
It can &e used onl$ inside a Do...Loop statement.Exit For
ro+ides a #a$ to e!it a For loop.
It can &e used onl$ in a For...Next or For Each...Next loop.
Exit Function Immediatel$ e!its the Function procedure in #hich it appears.
Exit ropert! Immediatel$ e!its the ropert! procedure in #hich it appears.
Exit SubImmediatel$ e!its the Sub procedure in #hich it appears.
8/10/2019 How to Do Programming in VBScript
15/18
>'
'ractice set
Input parameters names are From and To
Gssume that &oth inputs are integers.
Displa$ in reporter all the pairs &et#een For and To
se the For... e!t loop.
Tip se the Step statement.
Concatenate to a temporar$ string.
Tr$ $our program for the follo#ing +alues
From 7 2 - To 7 >8 H25"5(565>8From 7 > - To 7 ? H25"5(56
From 7 J - To 7 3 HJ(5J"5J25852
From 7 '8 - To 7 "> H'85"65"(5""5"2
From 7 J3 - To 7 J? HJ"5J(5J6
8/10/2019 How to Do Programming in VBScript
16/18
>(
'ractice set
Input parameters names are s#d.
Declare a constant A4K GSSW;)D.
Declare a constant AG K)ET)4 7 3.
If the pass#ord is correct displa$ a mic ass message.
If the pass#ord is t$ped #rong more then AG K)ET)4times5 displa$ a micFail message.
se the Do..Loop HWhile or ntil .
Tip
se the E!it Loop statement.
8/10/2019 How to Do Programming in VBScript
17/18
(a)e sure to *isit us
Tutorials
Grticles
ro ects
Gnd much more
>
8/10/2019 How to Do Programming in VBScript
18/18
Top Related