Do . . . Loop in VBScripting

This looping statement provides a great way to execute a group of statements until a condition is true.  There might be lot of situations where we can specify a condition but not the iterations (i.e. no minimum and maximum values or array containing elements). In such situations we can go ahead and use this loop.

Syntax:

Do {While (expression)} OR {Until (expression)}

—–

block of code

——

Loop

Example:

ans = MsgBox("Do you want to display multiplication table?? ", vbYesNo, "Confirm Continuation...")
    If ans=6 Then
        num=inputbox("Enter the number to which multiplication table to be displayed..")
    End If
mtable=""
Do While (ans=6)
            For i=1 to 10
               mtable=mtable&num&"X"&i&"="&num*i&vbnewline
            Next
        msgbox mtable
      ans = MsgBox("Do you want to display multiplication table?? starts from .. 2...", vbYesNo, "Confirm Continuation...")
          If ans=6 Then
            num=inputbox("Enter the number to which multiplication table to be displayed..")
          End If
      mtable=""
Loop

Above example displays a multiplication table for the number entered by the user until the user clicks Yes button on the popup.  If the user clicks on No then the condition becomes false and loop exits executing the block of code inside the Do . . . Loop.

Syntax 2:

Do

—–

block of code

——

Loop {While (expression)} OR {Until (expression)}

This syntax of the Do . . . Loop works a little bit different.  The block of code inside the first syntax gets executed if the condition is true otherwise the control does not get into the loop.  In the syntax 2 the control gets into the block of code inside the loop and executes once then checks the condition.  If it is true second iteration gets executed otherwise the control comes out of the loop.

This can be used where there is a need to execute the set of statements inside the loop at least once.

The While can be replaced by Until and they work a little bit different.  This will be demonstrated using an example.

Example:

mtable=""
Do 
       num=inputbox("Enter the number to which multiplication table to be displayed..")
        For i=1 to 10
               mtable=mtable&num&"X"&i&"="&num*i&vbnewline
        Next
        msgbox mtable
      ans = MsgBox("Do you want to display multiplication table?? starts from .. 2...", vbYesNo, "Confirm Continuation...")
      mtable=""
Loop While (ans=6)

The same example from the syntax –1 is used to demonstrate how the Do .. Loop While can be used when there is a need to execute the loop at least once.  The block of code inside the loop will be executed while the condition is true.  If we replace the While keyword with Until, the same code executes only once.

mtable=""
Do 
       num=inputbox("Enter the number to which multiplication table to be displayed..")
        For i=1 to 10
               mtable=mtable&num&"X"&i&"="&num*i&vbnewline
        Next
        msgbox mtable
      ans = MsgBox("Do you want to display multiplication table?? starts from .. 2...", vbYesNo, "Confirm Continuation...")
      mtable=""
Loop Until (ans=6)

Execute the above code, then click Yes button on the popup.  When Yes button is clicked execution will be stopped.  This is because the loop will be executed Until the condition is true.  Once the condition becomes true the control comes out of the loop.

Replace 6 with 7, again this code works fine.  Because vbYes = 6 and vbNo = 7.  If the user clicks on Yes then the value of ans becomes 6 and if the user clicks on No button then the value of ans becomes 7.  According to the expression ans = 7 (if 6 is replaced by 7) and the do loop gets executed until ans = 7.

The loop can be exited at any point in time during the execution with “Exit Do” keyword that is used with If .. Then .. EndIf conditional statement .

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.