VBScript - The conditional structures

January 2017

What is a conditional structure?

Conditional structures refers to the instructions that test whether a condition is true or not, that is to say, if the value of the expression is 0 or 1 (VBScript assign the value of 1 to the True keyword and 0 to False). These conditional structures can be associated with structures that are repeated according to the fulfillment of the condition (loop structures).

A block - The concept

An Expression located on a line of the script is called instruction. For example, a++ is an instruction.
If you need to group several instructions, you can create what is known as a block, that is to say a set of instructions on multiple lines.

The if, for and select statements are used to create your block of instructions.

The if ... then statement

The if ... then statement is the most basic test structure and is found in all languages ??(with a different syntax ...). It allows you to execute a series of statements when a condition is met. The syntax of this expression is as follows:
If condition Then instruction

When if statement is followed by several instructions, it is essential to end the series of instructions by the "End If" keyword. The syntax is:

If condition Then  

End If

Note that:
  • The condition must be enclosed in parentheses
  • it is possible to define several conditions to be met with AND and OR operators. For example, the following statement tests whether the two conditions are true:
    • if condition1 AND condition2
  • The following statement will execute instructions if either of the two conditions is true:
    • if condition1 OR condition2
  • if there is a single instruction, the End If keyword is not necessary ...

The if ... then ... else instruction

The if ... then ... else expression can be used to execute another set of instructions in the case of non-fulfillment of the condition.

The syntax of this expression is as follows:

If condition Then 

Instruction List 


Another set of instructions 

End If

If ... ElseIf ... else instruction

The If ... ElseIf ... else can be used when you need to test a set of conditions. It allows you to execute a series of instructions one after the other without the need to nest if statements.
The syntax of this expression is as follows:

If condition Then 

List of instructions 

Elseif other_condition Then 
other list of instructions 

last series of instructions 

End If 

The Select Case statement

The Select Case statement allows you to test several values ??of the contents of a variable. Its syntax is as follows:

Select Case</gras> (Expression)  

Case Value1:  
 list of instructions 

Case Value2:  
 list of instructions 

Case Value...:  
 list of instructions 

Case Else  

 list of instructions 

End Select 

When tested the expression is equal to a value in a case, and the list of instructions following it is executed.

The Loops

Loops are structures which can execute the same series of instructions (several times) until a condition is no longer met...
These structures are sometimes called "iterations".
The most common way to loop is to create a counter (a variable that increments: increases by 1 after each loop) and to stop the loop when the counter exceeds a certain value.

The For Loop

The For instruction can execute several times the same set of instructions: it is a loop!
In its syntax, simply specify the name of the variable that will be used as a counter, its initial value, the stopping value and the increments (or decrements).
The syntax of this expression is as follows:
For counter = Start To End [Step Increment] 
instruction list 

Next [counter]

For example:
For i = 1 To 5 

Print i 

  • This loop displays 5 times the value of i, that is to say, 1, 2, 3, 4, 5.
  • It starts at i = 1, checks if i is less than 6, etc ... and increments itself.
  • The (Step) can be positive or negative
  • Print a statement in your loop is a good way to check the value of the counter!
  • If the step is positive, the initial value must be smaller than the end
  • If the step is negative, the initial value must be greater than the end
  • You must count the number of times you want to execute the loop:
    • for i= 0 to 10, run the loop 11 times
    • for i=1 to 10, run the loop 10 times

The Do Loop instruction

The Do ... Loop (usually called a while in other languages) is another way to run multiple times the same set of instructions.
The syntax of this expression is as follows:

Do While condition 
Instruction List 


The For Each statement

The For Each statement works like For statement. It allows you to repeat a series of statements based on a series of values ??contained in an object or an array.
The syntax of this expression is as follows:
For Each Value In Values 

Instruction list 

Next Element

Stop script execution

VBScript allows the use of the exit command, which allows complete interruption of the script.

Original document published CommentcaMarche.net.


Published by jak58. Latest update on March 11, 2013 at 01:11 PM by jak58.
This document, titled "VBScript - The conditional structures," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).