Conditionals
Conditionals in Fantom are comprised of if-statements, if-else statements, and switch statements.
If-statements
If-statements in Fantom remain virtually the same to if-statements in it's parent languages, Java and C#. The if-statement takes some form of conditional and will only perform the block of code inside of it IF the condition stated is true. Every if-statement will be checked if performed in a sequence, however if-else statements will stop once the first true condition is met and perform the nested code. Else will only be performed if none of the previous conditionals have been satisfied.
Switch Statements
Switch statements remain very similar to Java and C# as well, however with some syntax differences. A switch condition is declared at the start and is compared to the list of case labels below it, executing code when a condition is met. If none of the conditionals are met, there is a default condition that works like the else of and if-statement. The default portion is optional in Fantom.
Expressions
There are many different type of operators that can be used for conditional statements. For comparing numeric variables or objects, there are the standard operators available.
- ==
- !=
- <
- >
- <=
- >=
- <=>
When comparing Strings or other Object types, there are some useful shorthand operators available in Fantom.
The .equals() method of Objects can be simplified to === or !== for does not equal. These are operators mean that the two compared objects are exactly the same, or are not exactly the same.
Multiple expressions can be used in one conditional statement using the conditional and (&&) or the conditional or (||) operators.



Comments
Post a Comment