"While" Statement Examples

This section provides a tutorial example on how to use 'while' statements to repeat a block of statements.

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

<!-- loop_while.html
 - Copyright (c) 1998 HerongYang.com. All Rights Reserved.
<script language="vbscript">
   i = 3
   While i <= 30
      bIsPrime = True
      j = 2
      While j <= i\2 And bIsPrime
         bIsPrime = i Mod j > 0
         j = j + 1
      If bIsPrime Then 
         document.writeln("Found a prime number: " & i)  
      End If
      i = i + 2

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?

Table of Contents

 About This Book

 Introduction of VBScript - Visual Basic Scripting Edition

 Variant Data Type, Subtypes, and Literals

 Arithmetic Operations

 Numeric Comparison Operations and Logical Operations

 String Operations - Concatenation and Comparison

 Variable Declaration and Assignment Statement

 Expression and Order of Operation Precedence

 Statement Syntax and Statement Types

 Array Data Type and Related Statements

 Array References and Array Assignment Statements

 Conditional Statements - "If ... Then" and "Select Case"

Loop Statements - "For", "While", and "Do"

 "For ... Next" Statements

 "For ... Next" Statement Example Examples

 "While" Statements

"While" Statement Examples

 "Do ... Loop" Statements

 "Function" and "Sub" Procedures

 Built-in Functions

 Inspecting Variables Received in Procedures

 Error Handling Flag and the "Err" Object

 Regular Expression Pattern Match and Replacement

 scrrun.dll - Scripting Runtime DLL Library

 Creating Your Own Classes

 IE Web Browser Supporting VBScript

 IIS ASP Server Supporting VBScript

 WSH (Windows Script Host)


 Full Version in PDF/EPUB