VB Script - A Widely Used Scripting Language

Share Embed Donate


Short Description

A scripting language or script language is a programming language that supports scripts, programs written for a special ...

Description

VB Script – A Widely Used Scripting Language

What we learn last session? Branching Branching using If …Then…Else statement. Branching using Select Case

Subjects for session 5

Looping through code. Do…Loop While…Wend For…e!t For…Each

E!it statement.

Looping hrough !ode

Looping allo#s $ou to run a group of statements repeatedl$. Some loops repeat statements until a condition is False% others repeat statements until a condition is True. There are also loops that repeat statements a specific num&er of times.

"o While#Until

Looping hrough !ode Using "o$Loop State%ent

)epeats a &loc* of statements #hile a condition is True or until a condition &ecomes True. The follo#ing looping statements are a+aila&le in ,BScriptDo /While 0 Until1 condition statements  Exit Do statements  Loop

Do

statements  Exit Do statements  Loop /While 0 Until1

condition 

Looping hrough !ode Using "o$Loop State%ent 4ou can e!it a Do...Loop &$ using the Exit Do statement. Because $ou usuall$ #ant to e!it onl$ in certain situations5 such as to a+oid an endless loop5 $ou should use the Exit Do statement in the True statement &loc* of an If ...Then ...Then... ...Else Else statement. If the condition is False5 the loop runs as usual.

Looping hrough !ode Using "o$Loop State%ent Dim &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. loop.

iCount 7 iCount = >

9 Increment Counter.

If  iCount  iCount 7 >8 Then 9 If condition is True...

&Chec* 7 False Exit Do

9 set +alue of flag to False. 9 E!it inner loop.

End If  Loop Loop Until &Chec* 7 False

9 E!it outer loop immediatel$.

Looping hrough !ode Using While$Wend State%ent E!ecutes a series of statements as long as a gi+en condition is True. While... ...Wend Wend statement is pro+ided in ,BScript for The While those #ho are familiar #ith its usage. While... ...Wend Wend5 @o#e+er5 &ecause of the lac* of fle!i&ilit$ in While Do... ...Loop Loop instead. it is recommended that $ou use Do While... ...Wend Wend statement is pro+ided in ,BScript for The While those #ho are familiar #ith its usage.

Looping hrough !ode Using &or$'e(t State%ent )epeats a group of statements a specified num&er of times. For... ...Next Next statements to run a &loc* of 4ou can use For statements a specific num&er of times.

For loops5 use a counter +aria&le #hose +alue increases or decreases #ith each repetition of the loop

Dim ! For ! 7 > To '8 A$roc Next

Looping hrough !ode Using &or$'e(t State%ent sing 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  is incremented &$ 2 each time the loop repeats. When the loop is finished5 the total is the sum of 25 "5 (5 65 and >8.

Dim 5 itotal For  7 2 To >8 Step 2 itotal 7 itotal =  Next Asg 7 The total is   itotal

Looping hrough !ode Using &or$'e(t State%ent 4ou can e!it an$ For...Next statement &efore the counter reaches its end +alue &$ using the Exit For statement. Because $ou usuall$ #ant to e!it onl$ in certain situations5 such as #hen an error occurs5 $ou should use the Exit For statement in the True statement &loc* of an If...Then...Else statement. If the condition is False5 the loop runs as usual.

Looping hrough !ode Using &or )ach***'e(t State%ent G 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 a group of statements for each item in a collection of o&ects or for each element of an arra$. This is especiall$ helpful if $ou don9t *no# ho# man$ elements are in a collection.

Looping hrough !ode )(it State%ent E!its a &loc* of Do...Loop5 For...Next5 Function5 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 Sub Immediatel$ e!its the Sub procedure in #hich it appears.

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF