Else

From EDukeWiki
Jump to navigation Jump to search

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
    }
}