If ElseIf Else

Navigation:  Widget Designer > Scripting > Script Language > Programming Statements >

If ElseIf Else

prev main next

Navigation:  Widget Designer > Scripting > Script Language > Programming Statements >

If ElseIf Else

prev main next

Please wait. Due to its content this page might take a while to load...

As in other programming languages, the if-then-else statement in Widget Designer can be used to program conditional actions. This means, that you may program to perform a certain action if a condition is "true" and another action if it isn't. The result whether something is "true" is based on a comparison of different values, e.g. "is value A bigger than value B?".
 
- A value can be a number (double, integer) or text (string); it can be a static value e.g. "10" or it can be a variable (holding "10").

- The comparison "bigger than" would be expressed with a logical operator ">". Four operators are supported to evaluate whether two values are equal or unequal and in case of a numeric values, if one is bigger or smaller than the other one.

- A entire condition can not only consist of one comparison of two values. It can be extended with a second comparison (of two other values) using the logical "AND" or "OR", e.g. "is value A bigger than value B and at the same time C smaller than D?"

- If a condition is true, the True Script is executed. It starts and ends with curly brackets {} and contains an unlimited number of commands including functions and macros.

- You may decide what happens if the condition is not true. If nothing should take place, the statement simply ends with the curly bracket behind the True Script. If something else should happen, it needs to be announced with the word "Else" followed by the {False Script}

- You can also add several possibilities with the "Elseif" statement, followed by the {Else True Script}

  If you have several "Elseif" statements, the switch case might be a better option.

Even encapsulated structures can be used, containing further if-else-statements, for-loops or switch cases.

Please note that you can also use mathematical expressions as if values.

Basic syntax: If A = B

If value A Operator value B {
 True Script
}

Elseif value A Operator value B {
 True Script
}

Else {
 False Script
}

The "operator value" could be "=", all available operators are listed further down.

It does not matter whether the curly brackets are in the same or next line, you could even write:
If value A Operator value B {True Script}
Else {False Script}

Or even shorter without "Else":
If value A Operator value B {True Script 1}
If value C Operator value B {True Script 2}

But it is strongly recommended to add new lines and also tabs for a clear and easy to read arrangement. Code blocks can be in/out-dented using [TAB] or [SHIFT + TAB].

If you like to comment a line out without getting errors in the Debug Logger, use this syntax:
// one line that should not be processed
/* several lines

that should not

be processed */

 

Script Example
This example changes the value of Fader1. The first position is at 128. The second position depends on the variable Var1. If Var1 equals 100, the fader goes down after waiting 2 seconds. If the variable is not 100, the fader goes up after 2 seconds. No matter what the second position was, the third position at 64 is set after waiting one second.

WDFaderValue(1,128)

if Var1 = 100 {

 WDWait(2)

 WDFaderDown(1,1)

}

 

Else {

 WDWait(2)

 WDFaderUp(1,1)

}

WDWait(1)

WDFaderValue(1,64)

 

The available operators

"=" equals

">" greater than

"<" lower than

"!=" not equal

">=" greater than or equals

"<=" lower than or equals

It is not necessary to put white spaces between values and operators, but advisable for better readability.

If you are simply requesting a Boolean value as if-statement, e.g. "if var_bool = true {...}", you can shorten this to "if var_bool {...}". Using a statement without operator automatically checks if the statement is true.

 

Syntax for advanced conditions: If A = B AND / OR C = D

Logical AND & OR are also possible in order to combine two sets of conditions:

If value A Operator value B AND value C Operator value D {
 True Script
}
Else {
 False Script
}

Alternatively the syntax could look as following:

If value A Operator value B OR value C Operator value D {

True Script

}

 

Script Example
This example changes the value of Fader1. The first position is at 128. The second position depends on the variables Var1 and Var2. Only if Var1 equals the word "Hello" and at the same time, Var2 equals the word "World", the fader goes down after waiting 2 seconds. If the variable is not 100, the fader goes up after 2 seconds. No matter what the second position was, the third position at 64 is set after waiting one second.

WDFaderValue,1,128

if Var1 = Hello AND Var2 = World {

 WDWait(2)

 WDFaderDown(1,1)

}

 

Else {

 WDWait(2)

 WDFaderUp(1,1)

}

WDWait(1)

WDFaderValue(1,64)