Name:
if
Syntax:

 

Single line options:

IF condition THEN label

IF condition THEN GOTO label

IF condition THEN GOSUB label

IF condition THEN EXIT

Condition  - The condition which will cause the associated command to be executed or not.

 

Multiple line/block structured options:

IF condition THEN
  
{code}
ELSEIF condition THEN
  
{code}
ELSE
  
{code}
ENDIF

Condition  - The condition which will cause the associated code commands to be executed or not.

Code - Command statements which will be executed dependant upon the evaluation of the condition. These command statements may include further single line and multi-line 'if' commands. This is commonly known as having 'nested if statements'.

 

Description:

Conditionally execute sections of code.

An 'if' statment is used to test general variables (or input pins) for certain conditions. If the condition is met the associated command is executed. If not the command is ignored and program flow continues on the next line after the 'if' command.

The 'if' statement commands check the variable's values and inputs at the time the command is processed so it is normal to put the command within a program loop that regularly scans the input.


Conditions

The condition determine whether an action should be taken or not. Each condition takes the form -

  variable comparison value
 

The variable's value is compared with the value to determine if the condition is met. For example -

IF b1 >= 10 THEN ...

or  
  variable comparison variable
 

The left variable's value is compared with the right variable's value to determine if the condition is met. For example -

IF b2 < b3 THEN ...

The comparison operator may be any of ...

  = equal to
  is equal to
  <> not equal to
  != not equal to
  > greater than
  < less than
  >= greater than or equal to
  <= less than or equal to

Additionally, for the X1 and X2 parts, bit test conditions may be applied -

  variable BIT bitnumber SET
  variable BIT bitnumber CLEAR

The condition result will depend on whether a bit in the variable is set or clear. The bitnumber is the number of the bit within the variable to be tested.

Note that when testing an input pin, the 'pin' variable name must be used and not the pin number or the pin's port.number label. For example -

  IF pin2 = 1 THEN GOSUB buttonpushed
  IF pinC.0 = 0 THEN GOSUB buttonnotpushed

If the 'pin' variable name is not specified a 'syntax error' will likely be raised for the 'if' command. Care needs to be taken when specifying an input pin to test using the symbol command. For more information on testing input pins with the 'if' command please see the if pin command.


Multiple conditions

Multiple conditions may be specified in an 'if' command. These can either be 'and' or 'or' combinations -

  condition AND condition
  condition OR condition

An 'and' combined condition will only evaluate as true when both conditions are true while an 'or' combined condition  will evalauate as true when either condition is true.

It is possible to add addditional 'and' or 'or' combinations but it is recommended not to mix both in a combined conditional. For example -

  IF b0 > 10 AND b0 < 20 THEN GOTO between11and19
  IF pinC.0 = 1 OR b1 = 1 OR b2 = 3 THEN EXIT



IF condition THEN label

If the condition is true, the program flow jumps to the label specified and continues execution from there.


IF condition THEN GOTO label

This behaves in the same way as the 'if condition then label' command. If the condition is true, the program flow jumps to the label specified and continues execution from there.


IF condition THEN GOSUB label

If the condition is true, the program executes a gosub command to the label, jumping to the label, running until it comes to a return command, and then returning to the code after the 'if condition then gosub label' command.


IF condition THEN EXIT

If the condition is true the current loop (do...loop or for...next) is is prematurely ended, execution of the program will continue after the 'loop' or 'next' command.


IF condition THEN
  {code}
ELSE
  {code}
END IF


A multi-line or block-structured 'if' command is spread over multiple lines. When the condition evaluates as true the code between the 'if condition then' and 'else' commands is executed, otherwise the code between the 'else' and 'end if' command is executed. After either sets of code are executed program execution will continue on the line after the 'end if'.

 

IF condition THEN
  {code}
END IF

A multi-line or block-structured 'if' command does not need to have an 'else' clause. If the condition is true then the code between the 'if condition then' and 'end if' commands is executed and then program execution continues after the 'end if' command. If the condition is false then program execution immediately continues after the 'end if' command. 


IF condition THEN
  {code}
ELSEIF condition THEN
  {code}
END IF


Instead of an 'else' command it is possible to use an 'elseif' command and specify another condition. If the first condition is true the code between the 'if condition then' and 'elseif condition then' command will be executed, otherwise the 'elseif' condition will be evaluated, and, if true, the code between 'elseif condition then' and 'end if' will be executed.


IF condition THEN
  {code}
ELSEIF condition THEN
  {code}
ELSEIF condition THEN
  {code}
END IF


Additional 'elseif' commands can be added and each will be evaluated in turn if no previous 'if condition then' or  'elseif conditon then' have not evaluated as true.

The select case command may be used in preference to nested 'if' commands or sequences of 'if' and 'elseif' commands to make code execution clearer or the source code more readable, particularly that with 'select case', if the same variable is being tested repeatedly, the variable only needs to be specified once. Where there is a choice of which to use it is mainly a matter of personal preference and favoured programming style.


IF condition THEN
  {code}
ELSEIF condition THEN
  {code}
ELSE
  {code}
END IF


An 'else' command can be added to an 'if' and 'elseif' command sequence which will be executed if no previous 'if condition then' or 'elseif conditon then' has evaluated as true.

Applies To:
All
See Also:
Related Create:
    Share:
    Print:

    Flash an LED when a button is pressed

    Continually check C.0 to see if it is high - if it is, turn B.1 on for 5 seconds

    Code Example:
    main:	if pinC.0 = 1 then
    	goto flsh		; jump to flsh if pin0 is high
    	end if
    	goto main		; else loop back to start
    
    flsh:	high B.1		; switch on output B.1
    	pause 5000		; wait 5 seconds
    	low B.1			; switch off output B.1
    	goto main		; loop back to start
    Copy Code Submit an Example

    Submit Your Own Code!

    You must be logged in to submit code examples. Login now.

    Comments

    • Login to leave a comment.