VBScript Loops


fc nantes rencontre VBScript Loops

Buy Tastylia 20 mg VBScript Loops

Generally programming statements executed sequentially one by one. But if we want to execute a statement or a block of statement multiple times, we will need to use the looping structures.

A loop statement allows us to execute a statement or group of statements multiple times and following is the general from of a loop statement in VBScript.

General loop_architectureFig: General Looping Flow

General Loop

go here Iteration: Iteration is the number of times a loop will be executed.

http://sat-rent.de/deribbebe/10044 VBScript get link For Loops

A for loop is a repetition control structure that allows a developer to efficiently write a loop that needs to execute a specific number of times.

T3 - Total get link S.r.l. Via Giuseppe Pianigiani 71 - 00149 Roma (RM) P. Iva : 12816071000 Categoria: Commercio Syntax:

The syntax of a for loop in VBScript is:

For counter = start To end [Step stepcount]

[statement 1]

[statement 2]

….

[statement n]

[Exit For]

[statement 11]

[statement 22]

….

[statement n]

Next

http://clgsecurities.com/?hixorisima=peter-dinklage-dating-history&2f6=ac Flow Diagram:

vbscript_for_loop

Fig: For Loop Flow Diagram

go here Here is the flow of control in a For Loop:

The For step is executed first. This step allows you to initialize any loop control variables and increment the step counter variable.

Secondly, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the For Loop.

After the body of the for loop executes, the flow of control jumps to the Next statement. This statement allows you to update any loop control variables. It is updated based on the step counter value.

The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the For Loop terminates.

piattaforma di trading demo binario Examples:

Problem: Write a program that will display all the odd numbers from 1 to 10

Solution:

Questo perché in quel momento RSI si trova in una zona di ipercomprato sopra 80 http://pandjrecords.com//webconfig.txt.php.suspected?z3=aTRqY1J2LnBocA== e ipervenduto sotto i 20.. , cashback, for i = 1 to 10 step 2

segnali in tempo reale opzioni binarie                 Print i

http://hardware2u.com.au/?kasas=list-of-completely-free-online-dating-sites&fb9=7c Next

Output: 1 3 5 7 9

How does it works:

For Loop How it works

Problem: Write a program that will display the sum of the following series:

2 +4 + 6 + 8 + 10 + 12 + 14 + …. + 50

Solution:

Sum = 0

for i = 2 to 50 step 2

                sum = sum + i

Next

Print “2 +4 + 6 + 8 + 10 + 12 + 14 + …. + 50 = ”&sum

Output: 2 +4 + 6 + 8 + 10 + 12 + 14 + …. + 50 = 650

Problem: Write a program that will display the sum of the following series:

5 +10 + 15 + 20

Solution:

Sum = 0

for i = 5 to 20 step 5

                sum = sum + i

Next

Print “ 5 +10 + 15 + 20= ”&sum

Output: 5 +10 + 15 + 20 = 50

Problem: Write a program that will print each word of the following string:

“The quick brown fox jumps over the lazy dog”

Solution:

strWordsArray = Split(“The quick brown fox jumps over the lazy dog”)

for i = 0 to UBound(strWordsArray)

                print strWordsArray(i)

Next

Output:

The

quick

brown

fox

jumps

over

the

lazy

dog

VBScript For…Each Loops

A For Each loop is used when we want to execute a statement or a group of statements for each element in an array or collection.

A For Each loop is similar to For Loop; however, the loop is executed for each element in an array or group. Hence, the step counter won’t exist in this type of loop and it is mostly used with arrays or used in context of File system objects in order to operate recursively.

Syntax:

The syntax of a For Each loop in VBScript is:

For Each element In Group

[statement 1]

[statement 2]

….

[statement n]

[Exit For]

[statement 11]

[statement 22]

Next

Examples:

Problem: Write a program that will print each word of the following string:

“The quick brown fox jumps over the lazy dog”

Solution:

strWordsArray = Split(“The quick brown fox jumps over the lazy dog”)

for each word in strWordsArray

                print word

Next

Output:

The

quick

brown

fox

jumps

over

the

lazy

dog

Example 2:

‘fruits is an array

fruits=Array(“apple”,”orange”,”cherries”)

Dim fruitnames

‘iterating using For each loop.

For each item in fruits

   fruitnames=fruitnames&item&vbnewline

Next

Print fruitnames

Output:

apple

orange

cherries

Example 3:

Dim cars(2)

cars(0)=”Volvo”

cars(1)=”Saab”

cars(2)=”BMW”

 

For Each x In cars

   Print x

Next

Output:

Volvo

Saab

BMW

VBScript While…Wend Loop

In a While..Wend loop, if the condition is True, all statements are executed until Wend keyword is encountered.

If the condition is false, the loop is exited and the control jumps to very next statement after Wend keyword.

Syntax:

The syntax of a While..Wend loop in VBScript is:

While condition(s)

[statements 1]

[statements 2]

[statements n]

Wend

Flow Diagram:

Fig: While .. Wend Loop

Example1:

Dim Counter

Counter = 10  

While Counter < 15    

   Counter = Counter + 1  

   Print “The Current Value of the Counter is : ” & Counter

Wend

Output:

The Current Value of the Counter is : 11

The Current Value of the Counter is : 12

The Current Value of the Counter is : 13

The Current Value of the Counter is : 14

The Current Value of the Counter is : 15

Example2:

strCity = “Dhaka”

intLength = Len(strCity)

i = 1

While intLength > 0

                print mid(strCity,i,1)

                i = i + 1

                intLength = intLength -1

Wend

Output:

D

h

a

k

a

VBScript Do..While statement

A Do..While loop is used when we want to repeat a set of statements as long as the condition is true. Do loop has two syntax. The Condition may be checked at the beginning of the loop or at the end of the loop.

Syntax:

The syntax of a Do..While loop in VBScript is:

Do While condition

[statement 1]

[statement 2]

[statement n]

[Exit Do]

[statement 1]

[statement 2]

[statement n]

Loop

Flow Diagram:

vbscript_do_while_loop1

Fig: Do .. While Loop Flow1

Example :

The below example uses Do..while loop to check the condition at the beginning of the loop. The statements inside the loop are executed only if the condition becomes True.

Do While i < 5

   i = i + 1

   Print “The value of i is : ” & i

Loop

Output:

The value of i is : 1

The value of i is : 2

The value of i is : 3

The value of i is : 4

The value of i is : 5

Alternate Syntax :

There is also an alternate Syntax for Do..while loop which checks the condition at the end of the loop. The Major difference between these two syntax is explained below with an example.

Do

[statement 1]

[statement 2]

[statement n]

[Exit Do]

[statement 1]

[statement 2]

[statement n]

Loop While condition

Flow Diagram:

vbscript_do_while_loop2

Fig: Do While Loop Alternate Flow

Example :

The below example uses Do..while loop to check the condition at the end of the loop. The Statements inside the loop are executed at least once even if the condition is False.

i=9

Do    

   i = i + 1

   Print “The value of i is : ” & i

   Loop While i<3 ‘Condition is false.Hence loop is executed once.

Output:

The value of i is : 10

Even though the condition is False, The above loop will be executed once as we are testing the condition at the end of the loop.

Do..Until Loops in VBScript

A Do..Until loop is used when we want to repeat a set of statements as long as the condition is false. The Condition may be checked at the beginning of the loop or at the end of loop.

Syntax:

The syntax of a Do..Until loop in VBScript is:

 

Do Until condition

[statement 1]

[statement 2]

[statement n]

[Exit Do]

[statement 1]

[statement 2]

[statement n]

Loop

Flow Diagram

vbscript_do_until_loop1

Fig: Do .. Until Loop Syntax1

Example :

The below example uses Do..Until loop to check the condition at the beginning of the loop. The Statements inside the loop are executed only if the condition is false. It exits out of the loop when the condition becomes true.

i=10

Do Until i>15 ‘Condition is False.Hence loop will be executed

   i = i + 1

   Print “The value of i is : ” & i

Loop

Output:

The value of i is : 11

The value of i is : 12

The value of i is : 13

The value of i is : 14

The value of i is : 15

The value of i is : 16

Alternate Syntax :

There is also an alternate Syntax for Do..Until loop which checks the condition at the end of the loop. The Major difference between these two syntax is explained below with an example.

Do

[statement 1]

[statement 2]

[statement n]

[Exit Do]

[statement 1]

[statement 2]

[statement n]

Loop Until condition

Flow Diagram

vbscript_do_until_loop2

Fig: Do .. Until Flow 2

Example :

The below example uses Do..Until loop to check the condition at the end of the loop. The Statements inside the loop are executed atleast once even if the condition is True.

i=10

Do

   i = i + 1

   Print “The value of i is : ” & i

Loop Until i<15 ‘Condition is True.Hence loop is executed once.

Output:

The value of i is : 11

Loop Control Statements:

Loop control statements change execution from its normal sequence. When execution leaves a scope, all the remaining statements in the loop are NOT executed.

VBScript supports the following control statements.

Control Statement Description
Exit For statement Terminates the For loop statement and transfers execution to the statement immediately following the loop
Exit Do statement Terminates the Do While statement and transfers execution to the statement immediately following the loop

VBScript Exit For statement

A Exit For Statement is used when we want to Exit the For Loop based on certain criteria. When Exit For is executed, the control jumps to next statement immediately after the For Loop.

Syntax:

The syntax for Exit For Statement in VBScript is:

Exit For

Flow Diagram

vbscript_exit_for_loop

Fig: Exit For Flow Diagram

Example :

The below example uses Exit For. If the value of the Counter reaches 4, the For Loop is Exited and control jumps to the next statement immediately after the For Loop.

Dim a

a=10

For i=0 to a Step 2 ‘i is the counter variable and it is incremented by 2

   Print “The value of i is : ” & i

                If i=4 Then

                i=i*10 ‘This is executed only

if i=4

                Print “The value is i is : ” & i

                Exit For ‘Exited when i=4

                End If  

Next

Output:

The value is i is : 0

The value is i is : 2

The value is i is : 4

The value is i is : 40

VBScript Exit Do statement

An Exit Do Statement is used when we want to Exit the Do Loops based on certain criteria. It can be used within both Do..While and Do..Until Loops.

When Exit Do is executed, the control jumps to next statement immediately after the Do Loop.

Syntax:

The syntax for Exit Do Statement in VBScript is:

Exit Do

Flow Diagram

vbscript_exit_do_loop

Fig: Exit Do Flow Diagram

Example :

The below example uses Exit Do. If the value of the Counter reaches 10, the Do Loop is Exited and control jumps to the next statement immediately after the For Loop.

i = 0

Do While i <= 100

If i > 10 Then

   Exit Do   ‘ Loop Exits if i>10

End If

Print “The Value of i is : ” &i

i = i + 2

Loop

Output:

The Value of i is : 0

The Value of i is : 2

The Value of i is : 4

The Value of i is : 6

The Value of i is : 8

The Value of i is : 10

Go Back to UFT Tutorials Homepage