beautypg.com

Campbell Scientific CR5000 Measurement and Control Module User Manual

Page 215

background image

Section 9. Program Control Instructions

9-9

If ... Then ... Else Statement

Allows conditional execution, based on the evaluation of an expression.

Syntax 1
If
condition Then thenpart [Else elsepart]

Syntax 2
If
condition1 Then

[statementblock-1]

[ElseIf condition2 Then

[statementblock-2] ]

[Else

[statementblock-n] ]

End If

Syntax 1 Description
The single-line form is often useful for short, simple conditional tests. Syntax
1 has these parts:

Part

Description

If

Begins the simple If...Then control structure.

condition

An expression that evaluates true (nonzero) or false (0 and
Null).

Then

Identifies actions to be taken if condition is satisfied.

thenpart

Statements or branches performed when condition is true.

Else

Identifies actions taken if condition is not satisfied. If the
Else clause is not present, control passes to the next
statement in the program.

elsepart

Statements or branches performed when condition is false.

The thenpart and the elsepart fields both have this syntax:

{statements | [GoTo] linenumber | GoTo linelabel }

The thenpart and elsepart syntax has these parts:

Part

Description

statements

One or more CRBasic statements, separated by colons.

Note

You can have multiple statements with a condition, but they
must be on the same line and separated by colons, as in the
following statement:

If A > 10 Then A = A + 1 : B = B + A : C = C + B

Syntax 2 Description

The block form of If...Then...Else provides more structure and flexibility than
the single-line form and is usually easier to read, maintain, and debug. Syntax
2 has these parts: