Skip to content
Developerhome

IF Statements and Operators

  Less than to read

You can incorporate if-else-then statements for conditional execution, enabling you to control the flow of your code based on specific conditions. Along with if statements, you can utilise operators for conditional evaluations.

if-else-then Structure

if statements allow you to conditionally execute actions or define different branches of code based on specific conditions. Here’s the basic structure of an if-else-then statement within an action:

"actions": {
  "conditionalAction": [
    {
      "if": {
        "condition": [condition],
        "then": [actions-to-execute-if-true],
        "else": [actions-to-execute-if-false]
      }
    }
  ]
}

In this structure:

  • [condition] represents the condition that will be evaluated using the available operators.
  • [actions-to-execute-if-true] represents the actions that will be executed if the condition is true.
  • [actions-to-execute-if-false] represents the actions that will be executed if the condition is false.

You can nest if-else-then statements within each other to create more complex conditional logic.

Operators for Conditional Evaluations

Web amendability provides a set of operators that facilitate conditional evaluations. These operators include:

  • eq (equal): Checks if two values are equal.
  • lt (less than): Checks if the left value is less than the right value.
  • gt (greater than): Checks if the left value is greater than the right value.
  • ge (greater than or equal): Checks if the left value is greater than or equal to the right value.
  • le (less than or equal): Checks if the left value is less than or equal to the right value.
  • ne (not equal): Checks if two values are not equal.

You can use these operators to compare values and make decisions based on the evaluation results.

Example: Conditional Execution within an Action

Here’s an example of how if-else-then statements can be used within an action:

"events": [ 
  {
    "onStateChange": [
      {
        "watchTarget": "id",
        "onChange": [
          {
            "if": {
              "gt": [
                {
                  "get": {
                    "state": "id"
                  }
                },
                {
                  "string": {
                    "value": "0"
                  }
                }
              ],
              "then": [
                {
                  "set": {
                    "properties": {
                      "spare_text_1": {
                        "disabled": false
                      }
                    }
                  }
                }
              ],
              "else": [
                {
                  "set": {
                    "properties": {
                      "spare_text_1": {
                        "disabled": true
                      }
                    }
                  }
                }
              ]
            }
          }
        ]
      }       
    ]
  }
]

In the example, it sets up an event listener for the “onStateChange” event, which triggers when the value of the “id” state changes.

The event listener includes an “if” statement that performs a comparison using the “gt” (greater than) operator. It compares the value of the “id” state with the string value “0”. If the value of “id” is greater than “0”, the “then” block is executed. Otherwise, the “else” block is executed.

In the “then” block, it sets the “disabled” property of the “spare_text_1” element to false, enabling it. In the “else” block, it sets the “disabled” property of the “spare_text_1” element to true, disabling it.

By utilising if-else-then statements and operators, you can create dynamic and flexible code that responds to different conditions within your JSON structure.