Iterative Constructs:


*      Looping allows you to run a group of statements repeatedly.

*      Some loops repeat statements until a condition is False;

*      Others repeat statements until a condition are True.

*      There are also loops that repeat statements a specific number of times.

*      Following are the Iterative Statements available in VB Script.

*      For … Next

*      For Each … Next

*      Do While/Until … Loop

*      Do … Loop While/Until

*      Do … Loop

*      While … Wend

For … Next:

*      Description:

*      Repeats a group of statements a specified number of times.

*      You can use For ... Next statements to run a block of statements a specific number of times.

*      For loops, use a counter variable whose value increases or decreases with each repetition of the loop

*      Syntax:

For i = 1 To N Step Inc

Statement1
                   Statement2
                   ……………..
                   ……………..
                   StatementN

Next

*      I is the Variable for maintaining index for the loop.

*      N is the number of times loop should iterate. Means loop statements should get iterates N number of times.

*      Using the Step keyword, you can increase or decrease the counter variable Inc by the value you specify.

*      Examples:

*      For For i = 1 To 50

               MsgBox (i)             ‘ Displays 1 to 50 Numbars

Next

*      For For i = 1 To 50 Step 2

               MsgBox (i)             ‘ Displays Even Numbers In Between 1 to 50

Next

*      For For i = 2 To 50 Step 2

               MsgBox (i)             ‘ Displays Odd Numbers In Between 1 to 50

Next

For Each … Next:

*      Description:

*      A For Each...Next loop is similar to a For...Next loop.

*      Instead of repeating the statements a specified number of times, a For Each...Next loop repeats a group of statements for each item in a collection of objects or for each element of an array.

*      This is especially helpful if you don't know how many elements are in a collection.

*      Syntax:

For Each I in Var

Statement1
                   Statement2
                   ……………..
                   ……………..
                   StatementN

Next

*      Here Var is a variable of type collection of Objects/Elements/Values or Array

*      Examples:

*      For Each i In [1, 2, 3]

               MsgBox (i)             ‘ Displays 1, 2, 3

Next

*      For Each i In arr(10)

               MsgBox (i)             ‘ Displays Array Elements in Arr

Next

Do While … Loop:

*      Description:

*      Repeats a block of statements while a Expression condition is True or until a condition becomes True.

*      Syntax:

Do [While | Until Expression]  
  
          Statement1
                             Statement2
                             ……………..
                             ……………..
                             StatementN

          [Exit Do]    

Loop

*      Examples:

*      Do While iCount < 20               ‘Repeats until False    
             
              iCount = iCount + 1         ‘ Increment Counter.

              MsgBox(iCount)               ‘ Displays Numbers from 1 to 19

Loop

*      Do Until iCount > 20                 ‘Repeats until True     
   
              iCount = iCount + 1         ‘ Increment Counter.

              MsgBox(iCount)               ‘ Displays Numbers from 1 to 20

Loop

Do … Loop While:

*      Description:

*      Repeats a block of statements while Expression condition is True or until a condition becomes True.

*      Syntax:

Do

          Statement1
                             Statement2
                             ……………..
                             ……………..
                             StatementN

          [Exit Do]    

          Loop [While | Until Expression]   

*      Examples:

*      Do

              iCount = iCount + 1         ‘ Increment Counter.

              MsgBox(iCount)               ‘ Displays Numbers from 1 to 19

Loop While iCount < 20            ‘Repeats until False   
              
*      Do

              iCount = iCount + 1         ‘ Increment Counter.

              MsgBox(iCount)               ‘ Displays Numbers from 1 to 20

*      Loop Until iCount > 20             ‘Repeats until True       
 
While … Wend:

*      Description:

*      Executes a series of statements as long as a given condition is True.

*      The While...Wend statement is provided in VBScript for those who are familiar with its usage.

*      However, because of the lack of flexibility in While...Wend, it is recommended that you use Do...Loop instead.

*      The While...Wend statement is provided in VBScript for those who are familiar with its usage.

*      Syntax:

While Expression

          Statement1
                             Statement2
                             ……………..
                             ……………..
                             StatementN

[Exit Do]    

          WEnd

*      Examples:

*      While iCount < 20                     ‘Repeats until False

              iCount = iCount + 1         ‘ Increment Counter.

              MsgBox(iCount)               ‘ Displays Numbers from 1 to 19

Loop           

Exit Statement:

*      Exits a block of Do...Loop, For...Next, Function, or Sub Statements.

*      Exit Do

*      Provides a way to exit a Do...Loop statement.

*      It can be used only inside a Do...Loop statement.

*      Exit For

*      Provides a way to exit a For loop.

*      It can be used only in a For...Next or For Each...Next loop.

*      Exit Function

*      Immediately exits the Function procedure in which it appears.

*      Exit Property

*      Immediately exits the Property procedure in which it appears.

*      Exit Sub

*      Immediately exits the Sub procedure in which it appears.

No comments:

Post a Comment