While…Wend - VBScript


While…Wend

The While...Wend command in VBScript allows for repeated execution of a block of code until a specified condition becomes false. It is commonly used for iterating over elements or performing repetitive tasks.

Syntax

While condition
    ' Code to be executed
Wend

where:

  • condition is any valid expression that evaluates to true or false.

Options/Flags

None.

Examples

Simple Iteration:

i = 1
While i <= 10
    WScript.Echo i
    i = i + 1
Wend

Complex Iteration with Exit/Resume:

dim i
for i = 1 to 10
    if i mod 2 = 0 then
        continue for
    end if

    WScript.Echo i
    if i > 5 then exit for
next

Common Issues

  • Infinite Loops: Ensure that the condition in the While statement eventually becomes false to avoid an endless loop.
  • Using Exit/Resume Incorrectly: Exit and Resume statements can be used to skip portions of the loop, but misusing them can lead to logical errors.

Integration

While...Wend can be combined with other commands for complex tasks:

  • Nested Loops: Use multiple While...Wend blocks to create nested loops for more intricate iterations.
  • Conditional Execution: Use If...Else statements within the loop to execute specific actions based on conditions.

Related Commands

  • Do...Loop
  • For...Next