VBScript Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.01

Loop Statements

Part:   1  2   3 

VB Script Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Data Types and Literals

Variables

Logic Operations

String Operations

Conditional Statements

Arrays

Loop Statements

Functions and Subroutines

Built-in Functions

Variable Inspection

... Table of Contents

(Continued from previous part...)

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?

(Continued on next part...)

Part:   1  2   3 

Dr. Herong Yang, updated in 2006
VBScript Tutorials - Herong's Tutorial Notes - Loop Statements