If Statement

Makes a decision regarding the program's flow based on a result returned by an expression.

Syntax

If bool-exp1 [ Then ]

[ statements1 ]

[ Elseif bool-exp2 [ Then ]

 [ statements2 ]... ]

[ Else

[ statements3 ] ]

Endif

Comments

If the result of the bool-exp1 is True, the statements1 are executed. If bool-exp1 is False, the next Elseif (if any) expression is evaluated. If the next Elseif expression is True, the corresponding statements are executed. If none of the expressions is true, any statements following the Else are executed. After the first statements are executed, control passes to the next statement following the Endif.

Example

If i > 5 Then

i=i+1

Endif

! if i was 3, now it is still 3

! if i was 6, now it is 7

! --------------------------------

If (i > 2 and i < 6) Then

i=i+1

Endif

! if i was 3, now it is 4

! if i was 6, now it is still 6

! --------------------------------

If i

i=i+1

Endif

! if i was 0, now it is still 0

! if i was 6, now it is 7

! --------------------------------

If i < 2 then

j=1

elseif (i = 2) then

j=2

elseif i=3

j=3

else

j=4

Endif

! if i is 1, j is 1

! if i is 2, j is 2

! if i is 3, j is 3

! if i is 6, j is 4

! --------------------------------

! nested if Example

If i < 2

if i =1

j=1

else

j=2

endif

elseif i=2

j=3

else

j=4

endif

! if i is 1, j is 1

! if i is 0, j is 2

! if i is 2, j is 3

! if i is 6, j is 4

See Also

#ifdef, Expressions