IF

Top  Previous  Next

Action

Allows conditional execution or branching, based on the evaluation of a Boolean expression.

 

 

Syntax

IF expression THEN

 

[ ELSEIF expression THEN ]

 

[ ELSE ]

 

END IF

 

 

Remarks

expression

Any expression that evaluates to true or false.

 

New is the ability to use the one line version of IF :

IF expression THEN statement [ ELSE statement ]

The use of [ELSE] is optional.

 

Also new is the ability to test on bits :

IF var.bit = 1 THEN

 

In V 2.00 support for variable bit index is added:

Dim Idx as Byte

For IDX = 0 To 7

If P3.IDX = 1 Then

  Print "1" ;

Else

 Print "0" ;

End if

Next

 

A new feature in V2 is the ability to use multiple tests:

If a > 10 AND A < 10 OR A = 15 Then

NOP

End if

It does not work with strings but only numeric conditions.

When you want to test on bytes you can also use the string representation:

Dim X As Byte

If X = "A" then ' normally you need to write :

If X = 65 Then 'so these two lines do the same thing

 

 

 

See also

ELSE , END IF

 

 

Example

Dim A As Integer

A = 10

If A = 10 Then                           'test expression

Print " This part is executed."         'this will be printed

Else

Print " This will never  be executed." 'this not

End If

If A = 10 Then Print "New in BASCOM"

If A = 10 Then Goto Label1 Else Print "A<>10"

 

Label1:

Rem The following example shows enhanced use of IF THEN

If A.15 = 1 Then                         'test for bit

Print "BIT 15 IS SET"

End If

REM the following example shows the 1 line use of IF THEN [ELSE]

If A.15 = 0 Then Print "BIT 15 is cleared" Else Print "BIT 15 is set"