If, Else If, and Else Statements in Trillium
Trillium provides many decision-making statements that help the flow of the smart contract based on certain logical conditions. Here, you will go over if, else if, else, and nested if else statements to control the flow based on the conditions.
The If/Else Operators in Trillium are the following:
- if statement
- else-if statement
- else statement
If Statements Explained
The if statement contains a boolean condition followed by a single or multi-line code block to be executed. At runtime, if a boolean condition evaluates to true, then the code block will be executed, otherwise not. You can specify if you want the false condition to be the deciding factor for a code block to be entered also
Else If Statement
Multiple else if statements can be used after an if statement. It will only be executed when the if condition evaluates to false (or true if specified with correct operator). So, either if or one of the else if statements can be executed, but not both.
Else Statement
The else statement can come only after if or else if statement and can be used only once in the if-else statements tree. The else statement cannot contain any condition and will be executed when all the previous if and else if conditions failed to be met.