Statements

Now reading version 17.0. For the latest, read: Statements for version 25

Conditional Statements

The main conditional statements supported by VBScript are:

  • If...Then
  • For Next Loop
  • Exit For
  • For Each Next
  • Do Loop
  • While...WEnd
  • Select Case

Care needs to be taken to code scripts that avoid infinite loops — that is, ensuring that the conditions will eventually be met.

The If...Then Statement

The syntax is:

If Condition Then
  'code
Else If AnotherCondition Then
  'code
Else
  'code
End If

The For Loop

The For Next statement repeatedly loops through a block of code. The basic syntax is:

For counter = start to end
  ' block of code here
Next

The Exit For

The Exit For statement exits a For loop prematurely.

For counter = start to end
  if condition then Exit For
Next

The For Each Loop

The For Each loop is a variation on the For loop which is designed to iterate through a collection of objects as well as elements in an array. The basic syntax is:

For Each ObjectVar in Collection
  ' block of code here
Next

The Do Loop

The Do Loop has several loop variations.

Do while until condition
  ' code block
Loop

...and;

Do
  ' code block
Loop while until condition

...and;

Do
  ' code block
Loop

The While...WEnd Loop

The While WEnd statement repeatedly loops through a block of code. The basic syntax is:

While until condition
  ' code block
WEnd

The Select Case Statement

You can use the SELECT statement if you want to select one of many blocks of code to execute:

Select case payment
  case "Cash"
    msgbox "pay cash"
  case "MasterCard"
    msgbox "pay by Mastercard"
  case Else
    msgbox "Unknown payment method"
end select

Expressions and Operators

An expression is a valid combination of constants, variables, literal values, operators, and function results. Expressions are used to determine the value to assign to a variable, to compute the parameter of a function, or to test for a condition. Expressions can include function calls.

VBScript has a number of logical, arithmetic, Boolean and relational operators. Since these operators are grouped by the order of precedence which is different from the precedence orders used by Basic, C, etc. For example, the AND and OR operators have precedence compared to the relational one.

Arithmetic Operators

+

Addition

-

Subtraction

*

Multiplication

/

Division

\

Division with an integer result

^

Exponentiation

Mod

Modulo

Comparison Operators (Lowest Precedence)

=

Test whether equal or not.

<>

Test whether not equal or not.

<

Test whether less than or not.

>

Test whether greater than or not.

<=

Test whether less than or equal to or not.

>=

Test whether greater than or equal to or not.

Is

Compares two object reference variables.

String Operators

&

Concatenation

Logical Operators

Not

Logical NOT

And

Logical AND

Or

Logical OR

XOR

 

Eqv

 

Imp

 

&

 

If you find an issue, select the text/image and pressCtrl + Enterto send us your feedback.
Note

The features available depend on your Altium product access level. Compare features included in the various levels of Altium Designer Software Subscription and functionality delivered through applications provided by the Altium 365 platform.

If you don’t see a discussed feature in your software, contact Altium Sales to find out more.

Content