IF … Statement

The IF statement of the OTScript language corresponds to the switch or case statement of other languages such as C, Javascript, or Java.

This page discusses:

See Also
Basics

Syntax



Use

The instruction evaluates the left expression, finds an IF case whose literal value matches the result of the left expression, or the ELSE case when specified.

The IF statement can be replaced by IFNOT, which evaluates the negation of the left expression, or by IFRE, which evaluates a regular expression.

Note: The code associated with the IF or ELSE case can be surrounded with ( ) (round brackets) or { } (curly brackets).

The return value is empty if no case matches the result of the left expression.

Note: The return type of all cases must be compatible, otherwise to avoid compilation errors you can return an empty value with a correct type in some cases.

Example1

TMP absolute := number < 0 IF TRUE: (-number) ELSE: number;
TMP hasFailed := message IFRE "(?i)Error|Failed": TRUE ELSE: FALSE;

Example2

Prerequisite: reqSpec is an element of Requirement Specification class.

reqSpec.getAllRequirements.{
   name
      IF "Req1":{
         EACH.description;
      }
      IF "Req2":{
         EACH.Title
      }
      ELSE:{
         EACH.name
      }
}
  • If the requirement is named Req1, the code returns the requirement description.
  • If the requirement is named Req2, the code returns the requirement title.
  • If the requirement is named differently, the code returns the requirement name.