SWAP statement

Purpose

Exchange the values of two variables of the same data type.

Syntax

SWAP var1, var2

Remarks

var1 and var2 are two variables of the same type. If you try to swap variables of differing types (for example, String and Integer, or Single-precision and Double-precision), a compile-time Error 482 occurs ("Data type mismatch").

SWAP is handy because a simple trading of values in two consecutive assignment statements does not get the job done:

a = b

b = a

By the time you make the second assignment, variable a does not contain the value it used to.  To do this without the SWAP statement requires a temporary variable and a third assignment:

temp = a

a = b

b = temp

SWAP can be used to swap the target values of pointers.  In addition, SWAP can also be used to swap the values of pointers themselves.