• home
  • forum
  • my
  • kt
  • download
  • Loop Statements

    Author: 2007-09-06 10:26:42 From:

    This chapter describes:

    • "For ... Next" Statements
    • "For ... Next" Statement Example
    • "While" Statements
    • "While" Statement Example
    • "Do ... Loop" Statements

    "For ... Next" Statements

    "For ... Next" is probably the most commonly used loop statement in VB. Here is its syntax:

       For loop_variable = initial_value To final_value [Step interval]
          statement_block (multiple statements)
       Next
    

    where "loop_variable" is a temporary variable used to control the loop, "initial_value" is the initial value to be assigned to the loop variable, "final_value" is the upper bound to stop the loop, and "interval" is the interval that controls how the loop variable to be updated at each iteration. Note that if "interval" is not specified, "1" will be used as the interval.

    If you want to know exactly how the "For ... Next" statement will be executed, here is my understanding:

    Step 1: "intial_value" is assigned to "loop_variable".

    Step 2: Comparing the current value of "loop_variable" against the "final_value".

    Step 3: If the current value of "loop_variable" is less than or equal to the "final_value", continue with Step 5.

    Step 4: If the current value of "loop_variable" is greater than the "final_value", terminate the loop.

    Step 5: Execute "statement_block" enclosed in the "For ... Next" loop.

    Step 6: Update the "loop_variable" with the current value plus the "interval"

    Step 7: Continue with Step 2.

    Note that the above description is based positive "interval". If "interval" is a negative value, the termination condition will be "the loop variable value is less than the final value.

    If you want to terminate the loop early, you can use the "Exit For" statement, which will terminate the loop immediately.

    "For ... Next" Statement Example

    To help you understand how "For ... Next" statements work, I wrote the following the example, loop_for.html:

    <html>
    <body>
    <!-- loop_for.html
       Copyright (c) 2006 by Dr. Herong Yang. http://www.herongyang.com/
    -->
    <pre>
    <script language="vbscript">
       document.writeln("")
       For i = 3 To 30 Step 2
          bIsPrime = True
          For j = 2 To i\2
             bIsPrime = i Mod j > 0
             If Not bIsPrime Then Exit For
          Next
          If bIsPrime Then 
             document.writeln("Found a prime number: " & i)  
          End If
       Next
    </script>
    </pre>
    </body>
    </html>
    

    Here is the output:

    Found a prime number: 3
    Found a prime number: 5
    Found a prime number: 7
    Found a prime number: 11
    Found a prime number: 13
    Found a prime number: 17
    Found a prime number: 19
    Found a prime number: 23
    Found a prime number: 29
    

    The output looks good. Some notes about the sample script:

    • Nested loops, one loop inside another loop, are used.
    • The outer loop goes through integers between 3 and 20, and skips even numbers with "Step 2"
    • The inner loop goes determines if the current integer "i" is a prime or not
    • The inner loop has a conditional "Exit For" statement to terminate the loop as soon as "i" has been determined as a non-prime number.

    "While" Statements

    Another type of loop statements is called "While" statement, which has the following syntax:

       While condition
          statement_block (multiple statements)
       Wend
    

    where "condition" is a Boolean value.

    The way a "While" statement is executed looks like:

    Step 1: Check Boolean value of "condition".

    Step 2: If the current value of "condition" is "True", continue with Step 4.

    Step 3: If the current value of "condition" is "False", terminate the loop.

    Step 4: Execute "statement_block" enclosed in the "While ... Wend" loop.

    Step 5: Continue with Step 1.

    The logic of a "While" statement is simpler than a "For ... Next" statement. But you have to manage the condition carefully, so that its value will become "False" at some point to terminate the loop.

    Notice that there seem to be no "Exit" statement to break "While" loop early.

    "While" Statement Example

    To help you understand how "While" statements work, I wrote the following the example, loop_while.html:

    <html>
    <body>
    <!-- loop_while.html
       Copyright (c) 2006 by Dr. Herong Yang. http://www.herongyang.com/
    -->
    <pre>
    <script language="vbscript">
       document.writeln("")
       i = 3
       While i <= 30
          bIsPrime = True
          j = 2
          While j <= i\2 And bIsPrime
             bIsPrime = i Mod j > 0
             j = j + 1
          Wend  
          If bIsPrime Then 
             document.writeln("Found a prime number: " & i)  
          End If
          i = i + 2
       Wend
    </script>
    </pre>
    </body>
    </html>
    

    Here is the output:

    Found a prime number: 3
    Found a prime number: 5
    Found a prime number: 7
    Found a prime number: 11
    Found a prime number: 13
    Found a prime number: 17
    Found a prime number: 19
    Found a prime number: 23
    Found a prime number: 29
    

    The example worked perfectly. What do you think?

    "Do ... Loop" Statements

    Personally, I think that two types of loops, "For ... Next" and "While", are enough for any loop programming situations. But the designers of VB decided to give us an extra type of loops, "Do ... Loop" statements, with the following different flavors:

    1. "Do While ... Loop" Statements

       Do While condition
          statement_block
       Loop
    

    They are identical logically to:

       While condition
          statement_block
       Wend
    

    2. "Do Until ... Loop" Statements

       Do Until condition
          statement_block
       Loop
    

    They are identical logically to:

       While Not condition
          statement_block
       Wend
    

    3. "Do ... Loop While" Statements

       Do
          statement_block
       Loop While condition
    

    They are identical logically to:

       statement_block
       While condition
          same_statement_block
       Wend
    

    4. "Do ... Loop Until" Statements

       Do
          statement_block
       Loop Until condition
    

    They are identical logically to:

       statement_block
       While Not condition
          same_statement_block
       Wend
    

    I will leave it to you to write some sample scripts for "Do ... Loop" statements as exercises.

    Note that VB offers "Exit Do" statements to allow you to terminate "Do ... Loop" statements early.

    Conclusions

    Question: How many ways of writing a loop in VB? My answer is 7:

    • "For ... Next".
    • "While ... Wend".
    • "Do While ... Loop".
    • "Do Until ... Loop".
    • "Do ... Loop While".
    • "Do ... Loop Until".
    • "For Each ... Next" - Used with arrays and collections. See the array chapter for details.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      .NET (8)
      Buttons (3)
      Database Related (7)
      Date and Time (1)
      Development (3)
      Error Handling (2)
      File Manipulation (5)
      Introduction to Visual Basic (9)
      Miscellaneous (2)
      Multimedia (9)
      Networking (9)
      Security (1)
      VB Script (6)

    New

    Hot