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          ' <<< the If...Then...Else this page documents
        Set LIGHT Off
      Else
        Set LIGHT On
      End If
    Loop

Key line: If ReadAD(SENSOR) > 128 Then — the condition is evaluated once; the Else branch runs only when it is false.

See Also:

  • Conditions — background on how conditions are evaluated
  • Select Case — alternative when choosing between more than two branches
  • Do — looping construct used to repeat the check in the example above
  • For — fixed-count loop alternative to Do
  • ReadAD — reading the sensor value tested in the example above
  • Performance — why nested If can outperform a combined And/Or condition