if Instruction

The if instruction executes Jade instructions dependent upon a condition.

Syntax

The forms of the Jade conditional instruction are:

Description

Use the if instruction to perform conditional execution, based on the evaluation of an expression.

Examples

The following example shows a series of nested if instructions.

getJobTitle(level: Integer): String;
begin
    if level = 1 then
        return "CEO";
    else
        if level = 2 then
            return "Project Manager";
        else
            if level = 3 then
                return "Analyst";
            else
                if level = 4 then
                    return "Programmer";
                else
                    return "Invalid level";
                endif;
            endif;
        endif;
    endif;
end;

This can be written in a more compact and readable way, by making use of the elseif clause, as shown in the following example.

getJobTitle(level: Integer): String;
begin
    if level = 1 then
        return "CEO";
    elseif level = 2 then
        return "Project Manager";
    elseif level = 3 then
        return "Analyst";
    elseif level = 4 then
        return "Programmer";
    else
        return "Invalid level";
    endif;
end;