Chapter 5. Expressions, Statements, and Operators

Expressions

Unlike the Java programming language, the JavaFX Script programming language is an expression language. All executable statements are expressions which consist of zero or more inputs followed by a single output. This includes conditionals, loops, and even blocks. The following demo provides an example:

import java.lang.Math;
import java.lang.System;

var rand = (Math.random() * 100).intValue();
var s:String = null;
if (rand % 2 == 0) {
     s = "rand is even";
} else {
     s = "rand is odd";
}

System.out.println(s);

In the above example the then and else clauses of the conditional "if" are expressions in their own right, namely block expressions

Block Expressions

A block expression consists of a list of statements (which can be declarations or expressions) surrounded by curly braces and separated by semicolons. If the last statement is an expression, then the value of a block expression is the value of the last expression; otherwise the block expression has void type.

Therefore, the previous example could also be written as:

import java.lang.Math;
import java.lang.System;

var rand = (Math.random() * 100).intValue();
var s:String =
if (rand % 2 == 0) {
     "rand is even";
} else {
     "rand is odd";
};

System.out.println(s);

Alternately the braces can be omitted:

import java.lang.Math;
import java.lang.System; 

var rand = (Math.random() * 100).intValue();
var s:String = if (rand % 2 == 0) "rand is even" else "rand is odd";

System.out.println(s);

The Java programming language contains both an if statement, and a conditional expression: a < b ? a : b.

Thanks to block expressions, the JavaFX Script programming language if expression takes the place of both.

Range Expressions

Chapter 1 introduced you to sequences; chapter 6 covers them in full detail. But even with only a basic understanding of sequences, it should be possible to understand range expressions. Range expressions define a sequence of values forming an arithmetic series using the following syntax:

[number1..number2]

Such an expression defines a sequence whose elements consist of the integers from number1 to number2 (inclusive).

A simple example of a range expression could be:

import java.lang.System;

var nums = [0..3];
System.out.println(nums == [0,1,2,3]); // prints true

By default the interval between the values is 1 but it is also possible to specify a different interval by including the next number in the sequence after number1 separated by a comma. For example, the following expression defines an sequence consisting of the odd numbers between 1 and 10:

[1,3..10]

To create a descending range, make sure the second value is less than the first, and specify a step value:

import java.lang.System;

var nums = [3..0 step -1];
System.out.println(nums == [3,2,1,0]); // prints true

Note that the following declarations actually declare empty sequences:

var nums1 = [3..0 ]; 
var nums2 = [3..0 step 1]; 

Conditional Expressions

The if expression is like the expression in the Java™ programming language, except that curly braces are always required around the then and else clauses, unless the the else clause is another if expression.

if (condition1) {
     System.out.println("Condition 1");
} else if (condition2) {
     System.out.println("Condition2");
} else {
     System.out.println("not Condition 1 or Condition 2");
}

Looping Expressions

The for Expression

The while Expression

Statements

The return statement is the same as found in the Java programming language:

function add(x, y) {
     return x + y;
}

The throw statement is similar to that of the Java Programming language, but only objects extending java.lang.Throwable may be thrown and caught:

import java.lang.Exception;

function foo() {
     throw new Exception("this is a java exception");
}

function bar() {
     throw "just a string";
}

The try and catch statements are similar to those of the Java programming language, but use JavaFX Script programming language variable declaration syntax:


try {
     throw "Hello";
} catch (s:String) {
     System.out.println("caught a String: {s}");
} catch (any) {
     System.out.println("caught something not a String: {any}");
} finally {
     System.out.println("finally...");
}

The break and continue statements are similar to those of the Java Programming language; however labels are not supported. The break and continue statements must appear inside the body of a while or for loop:

Operators

The JavaFX Script programming language provides standard operators similar to those found in the Java programming language. The following chart lists these operators by precedence, comparing to their Java programming language equivalent.

Table 5.1. Operator Precedence Table
Priority JavaFX Operator Operation Java Operator Order of Evaluation
1 = assign operator = Right to Left
2 += add and assign +=
-= subtract and assign -+
*= multiply and assign *=
/= divide and assign /=
%= remainder and assign %=
/= divide and assign /=
3 and logical and && Right to Left
4 or logical or || Right to Left
5 instanceof inheritance operator instanceof Right to Left
as cast operator n/a
sizeof array length n/a
indexof ordinal position n/a
new instantiation new
op() function call n/a
x.op() member function call
6 = = equality = = Left to Right
< > inequality !=
<= less than or equal <=
<-  
< > less than / greater than < >
7 + addition + Right to Left
- subtraction; uniary negation -
8 * multiplication * Right to Left
/ division /
% remainder
9 ++ (prefixed) assign ++ Right to Left
-- (prefixed) assign --
10 ++ (suffixed) assign ++ Right to Left
-- (suffixed) assign --

Because these operators provide standard functionality, a simple example of their use should be all that is required:

import java.lang.System;
import java.lang.Math;

var x = 2;
var y = 4;
var a = true;
var b = false;
System.out.println(x == y);  
System.out.println(x <> y); 
System.out.println(x < y); 
System.out.println(x > y);  
System.out.println(x >= y); 
System.out.println(x <= y);  
System.out.println(x + y);  
System.out.println(x - y);  
System.out.println(x * y);  
System.out.println(x / y);   
System.out.println(x % y);   
System.out.println(a and b); 
System.out.println(a or b);  
System.out.println(not a);  
System.out.println(sizeof [x,y]); 
System.out.println([x,y][e | indexof e == 0]);  
System.out.println(if (a) x else y); 
System.out.println(for(q in [x, y] where q < 3) q); 
System.out.println(Math.max(x, y)); 
System.out.println("abc".toUpperCase()); 
System.out.println(x); 

The demo above produces the following output:


false
true
true
false
false
true
6
-2
8
0
2
false
true
false
2
[ 2 ]
2
[ 2 ]
4
ABC
2