Page tree
Skip to end of metadata
Go to start of metadata



Introduced in

Version:1.00

Description

Description:Executes the specified code, depending on the result of a defined condition.

The condition can be either a variable, or a statement, which returns a Boolean value (true or false).

While some conditions can be interpreted without parentheses (e.g. if alive player then...), other conditions do not work without them (e.g. if (_a==5) then...). So, in general, and for improved readability, it is recommended to always use parentheses.

Multiple conditions can be combined via the or and and clauses, and must be enclosed in parentheses:

if (alive tank1) then ...
if ((alive tank1) && (alive tank2)) then ...
if ((alive tank1) || (_deadcount>10)) then ...

If multiple conditions are defined, then all are evaluated before either code section is executed.
To prevent this, and to only evaluate further conditions that can affect the final outcome, a "short-circuit evaluation" can be used, be enclosing additional conditions in curly brackets (V2.0+):
if ((alive tank1) && {alive tank2}) then ...
Condition #2 (alive tank2) is only evaluated if condition #1 is true, as otherwise (with an && operand) the overall result would already be false, no matter what the 2nd condition returns.
if ((alive tank1) || {alive tank2}) then ...
Condition #2 (alive tank2) is only evaluated if condition #1 is false, as otherwise (with an || operand) the overall result would already be true, no matter what the 2nd condition returns.
Be aware though, that this type of evaluation comes with a performance penalty of about 10% (as counter-intuitive as this may be), so it should only be used in situations that are not time-critical.

If the condition returns an isNil value, then none of the assigned code sections are executed.

The code sections (for true or false condition) can either execute code, return a value, or do both. To return a value, it has to be the last (or only) element in the code section, and not be followed by a semi-colon.

Any local variables declared in the if...then code block is only available within that scope:

if (alive tank1) then {
  _state = 1;
} else {
  _state = 0;
};
// '_state' is undefined at this position 

To avoid this, the variables must be initialized beforehand - either via the private command or by assigning a value to them, e.g.

_state = -1;
if (alive tank1) then {
  _state = 1;
} else {
  _state = 0;
};
// '_state' now contains either 0 or 1.

Syntax

Syntax:if (condition) then {trueCode} else {falseCode}
Parameters:
  • condition: Boolean - Expression (code or variable) returning true or false.
  • trueCode: Code - Code to be executed if 'condition' returns true.
  • falseCode: Code - Code to be executed if 'condition' returns false. Can be omitted if the preceding 'else' statement is left out as well.
Return Value:Anything - Value returned by either 'trueCode' or 'falseCode'.

Alternative Syntax

Syntax:if (condition) then [ {trueCode} , {falseCode} ]
Parameters:
  • condition: Boolean - Expression (code or variable) returning true or false.
  • trueCode: Code - Code to be executed if 'condition' returns true.
  • falseCode: Code - Code to be executed if 'condition' returns false.
Return Value:Anything - Value returned by either 'trueCode' or 'falseCode'.

Examples

Examples:
// Only 'trueCode' is defined
if (alive tank1) then {hint 'Destroy tank'};

// 'trueCode' as well as 'falseCode' are defined:
if (alive tank1) then {
  hint 'Destroy tank'
} else {
  hint 'Tank already destroyed'
};

// code sections only return a string value (which is assigned to a variable)
_state = if (alive soldier1) then {"alive"} else {"dead"};
player sidechat format["Target is %1",_state];

// Conditional code, as well as return values
_state= if (alive soldier1) then {
  soldier1 setDamage 1; 
  "has been killed"
} else {
  "already dead"
}; 
player sidechat format["Target %1",_state];

// Alternative syntax (using square brackets)
_state = if (alive soldier1) then [{"alive"},{"dead"}];
player sidechat format["Target is %1",_state];

_state = if (alive soldier1) then [{soldier1 setDamage 1; "has been killed"}, {"already dead"}];
player sidechat format["Target %1",_state];

Additional Information

See also: if...then...elseif, switch, Control Structures
Multiplayer: 
Problems: 

Notes