beautypg.com

If ... then ... else statement – Campbell Scientific CR9000X Measurement and Control System User Manual

Page 340

background image

Section 9. Program Control Instructions

If ... Then ... Else Statement

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

There are two forms of the If .. Then construct: The Single Line form and the
Block form.

The single-line form is often useful for short, simple conditional tests.

The block form provides more structure and flexibility than the single-line
form and is usually easier to read, maintain, and debug.

Syntax 1 (Single Line Form)

If

condition

Then

thenpart [

Else

elsepart]

Syntax 1 Description
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.

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:

TIP

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

Syntax 2 Block form

If

condition1

Then

[statementblock-1]

[

ElseIf

condition2

Then

[statementblock-2] ]

[

Else

[statementblock-n] ]

End If

9-10