Else: Difference between revisions
No edit summary |
Doom64hunter (talk | contribs) Added section about peculiar 'else' logic in CON |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
if<type> { do something } '''else''' { do something else } | |||
Specifies code to run if a | Specifies code to run if a preceding 'if' condition is ''not'' met. See [[if condition]]. | ||
== Potentially Unexpected Behavior == | |||
Note that the 'else' in CON works differently to the 'else' you'd find in programming languages such as C++ or Java. | |||
'''Without the use of brackets, the `else` block is always associated with the most recently executed 'if' statement in the same scope.''' This means that if you have two contiguous 'if' statements like such: | |||
ife TEMP 1 | |||
ife TEMP2 1 | |||
{ | |||
nullop | |||
} | |||
else | |||
{ | |||
addlogvar TEMP | |||
} | |||
'''Then the 'else' block will be executed whenever either the first ''OR'' the second 'if' condition evaluates to ''false'''''. | |||
This behavior is unlike what one would expect from other languages, where the 'else' is only executed if and only if the second 'if' statement evaluates to ''false''. | |||
To avoid this behavior in CON, simply enclose the body of the first 'if' in brackets. Here, the 'else' ''will not'' trigger if the first 'if' evaluates to ''false'': | |||
ife TEMP 1 | |||
{ | |||
ife TEMP2 1 | |||
{ | |||
nullop | |||
} | |||
else | |||
{ | |||
addlogvar TEMP | |||
} | |||
} | |||
[[Category:Duke3D 1.3/1.5 commands]] |
Latest revision as of 00:12, 4 October 2019
if<type> { do something } else { do something else }
Specifies code to run if a preceding 'if' condition is not met. See if condition.
Potentially Unexpected Behavior
Note that the 'else' in CON works differently to the 'else' you'd find in programming languages such as C++ or Java.
Without the use of brackets, the `else` block is always associated with the most recently executed 'if' statement in the same scope. This means that if you have two contiguous 'if' statements like such:
ife TEMP 1 ife TEMP2 1 { nullop } else { addlogvar TEMP }
Then the 'else' block will be executed whenever either the first OR the second 'if' condition evaluates to false.
This behavior is unlike what one would expect from other languages, where the 'else' is only executed if and only if the second 'if' statement evaluates to false.
To avoid this behavior in CON, simply enclose the body of the first 'if' in brackets. Here, the 'else' will not trigger if the first 'if' evaluates to false:
ife TEMP 1 { ife TEMP2 1 { nullop } else { addlogvar TEMP } }