If

Syntax:

Short form:

    If condition Then command

Long form:

    If condition Then
    ...
    program code
    ...
    End If

Using Else:

    If condition Then
      code to run if true
    Else
      code to run if false
    End If

Using If Else:

    If condition Then
      code to run if true
    Else if nextcondition then
      code to run if nextcondition true
    Else
      code to run if false
    End If

Command Availability:

Available on all microcontrollers.

Explanation:

The If command is the most common command used to make decisions. If condition is true, then command (short) or program code (long) will be run. If it is false, then the microcontroller will skip to the code located on the next line (short) or after the End If (long form).

If Else is used, then the condition between If and Else will run if the condition is true, and the code between Else and End If will run if the condition is false.

If Else if is used, then the condition after the Else if will run if the condition is true.

Note: Else must be on a separate line in the source code.

Supported:

    <instruction> 'is supported
    Else
    <instruction>
    <instruction> Else 'Not Supported, but will compile
    <instruction>

Example:

    'Turn a light on or off depending on a light sensor

    #chip 12F683, 8

    #define LIGHT GPIO.1
    #define SENSOR AN3
    #define SENSOR_PORT GPIO.4

    Dir LIGHT Out
    Dir SENSOR_PORT In

    Do
      If ReadAD(SENSOR) > 128 Then
        Set LIGHT Off
      Else
        Set LIGHT On
      End If
    Loop

For more help, see Conditions