if-else

Syntax

if(condition){ 
     statements 
} 
else{ 
     statements 
}

Details

The parentheses after "if" are required. If more than one statement are to be executed in case a condition is true, we must specify a block of statements with braces { }. Otherwise the braces are optional.

Please note that if the condition is NULL or !NULL, it will be processed as false and execute the statement in else code block.

Examples

def temp(const a) {
    if (a>10)
        return a\10  //braces are optional if only need to execute one statement
    else if (a<=10 && a>1)
        return a
    else{
        b=abs(a)*10  //must use braces as we need to execute more than one statement
        return b
    }
};

temp 10;
// output
10
temp 11;
// output
1.1
temp 0.5
// output
5