Typically, each expression takes one line, but you can continue an expression onto the next line if you need more space.
The formula below consists of five expressions. It returns the Number value 25 since that is the value of the last expression in the formula.
Example
//Expressions example //The first expression. Its value is the Number //value 30 10 + 20; //The second expression. Its value is the String //"Hello World". It takes up two lines. "Hello " + "World"; //The third expression. Its value is of Number type {Orders Detail.Quantity} * 2 - 5; //The fourth expression. Its value is of String type If {Orders Detail.Quantity} > 1 Then "multiple units" Else "one unit"; //The fifth and final expression. Its value is the //Number value 25 20 + 5
Placing a semicolon after the last expression in the formula is also allowed, but is optional. For example, the above formula could have ended with:
20 + 5;
Some of the sample formulas in the Expressions section do not have semicolons. This is because they consist of a single expression, and a semicolon is optional after the last expression. Many formulas in Crystal syntax can be written as a single expression.
Notice that there is no semicolon after the "multiple units" string. In fact, if you put a semicolon there, the program will report an error. This is because a semicolon separates expressions, and the
Else "one unit";
is not a separate expression. It does not stand alone apart from the If. In fact, it is an integral part of the If expression since it describes the value that the If expression will return under certain circumstances.
Note The example is not a practical example because the first four expressions in the formula did not have any effect on the last expression.
The fact that a Crystal syntax formula is a sequence of expressions whose result is the value of the final expression is the most important concept in understanding Crystal syntax. This expression-based syntax allows you to write very short formulas with a lot of functionality.
Example
//First expression. It declares the Number variable x //and then returns the value of an uninitialized //Number variable, which is 0. NumberVar x; //Second expression. It assigns the value of 30 to x, //and returns 30. x := 30
The above formula would give an error if the first expression were omitted. This is because the second expression refers to the Number variable x, and the program needs to have x declared before it understands expressions involving it.
In general, you use variables to get the earlier expressions in a formula to affect the final expression. See Variables.