Skip to main content

Post 4: Conditional Statements

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

Popular posts from this blog

Post 5: Repeated Action Syntax

Loops While Loops While loops execute while the given conditional remains true. While loops use the same syntax as while loops in C# and Java.  The code will cease to execute once the given conditional is no longer true. For Loops For loops execute for a set, given length. A for loop will iterate through a range of given integers until the end of that range is reached. For loops are written the same was as in Java and C#.  For loops have the benefit of being able to keep track of how far something has looped through without the need for a separate variable by using the 'i', or however it may be named. For loops do not need to strictly follow hard coded numbers and could loop through the length of an array or size of a list or to any integer value. Iteration Iteration is a key function of lists in Fantom. By using the 'listName.each' method, you are able to iterate through the length of the list and execute code for each item in that list. This c...

Post 1: Background Information and Resources

Language Background Fantom is an object-oriented programming language designed to work in both the Java Runtime Environment and in the .NET Common Language Runtime while providing a more refined API that maintains much of the same functionality.  Fantom's syntax is relatively similar to that of Java and C# while refining certain aspects of the syntax for functionality and simplicity.  System.out.println("Hello world!"); becomes  echo("Hello world!") Fantom is designed to be portable between the Java and .NET VMs while also holding support for being compiled to JavaScript for browser use, making Fantom versatile.  Creators Fantom was created in 2005 by Brian Frank and Andy Frank and was originally named Fan. When the language began to gain some popularity, the name was changed to Fantom in order to address concerns about the ability to search for resources on a language named "Fan". The creators say that Fantom was created in orde...