Conditional code execution allows more flexible functionality with the SLAX if statement. Using the if statement instructs the script engine to execute segments of code only when certain conditions are met. This means that scripts can react to values instead of only operating on them.

If statements
The if statement consists of two parts: a boolean expression and a conditional code block:
if( $mtu == 1500 ) {
    <output> "Jumbo Frames are not enabled";
}
In the above example the boolean expression is $mtu == 1500. It is expressed within parenthesis immediately following the if statement. When the expression evaluates to true then the conditional code block of the if statement is executed. When the expression evaluates to false then the script engine skips to the end of the statements code block and continue processing from that point. In the example, if the $mtu variable is assigned to the value 1500 then <output> "Jumbo Frames are not enabled" is written to the result tree, otherwise this string does not appear in the console output.

Else if and else statements
Additional possibilities can be expressed by adding else if and/or else statements:
if( $interface == "fxp0" ) {
    <output> "Out of Band Management";
}
else if( $interface == "lo0" ) {
    <output> "Loopback Interface";
}
else {
    <output> "Other";
}
In this case, the script engine checks each boolean expression sequentially. The first expression that evaluates to true has its code block executed. If neither the if nor the else if evaluate to true, then the else code block is executed (when present). In all cases, the script engine executes a maximum of one conditional code block. If multiple boolean expressions evaluate to true, the script engine only applies the first.

version 1.0;

ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";

import "../import/junos.xsl";

/* This is a global parameter with a default value */
param $interface = "fxp0";

match / {
    <op-script-results> {
        if( $interface == "fxp0" ) {
            <output> "Out of Band Management";
        }
        else if( $interface == "lo0" ) {
            <output> "Loopback Interface";
        }
        else {
            <output> "Other";
        }
    }
}