Statements

Statements form complete units of execution. Statements end with a semicolon (;).

The ARL rule language supports basic statements and control statements.

Basic statements

The following types of expression can also be used as basic statements by ending the expression with a semicolon:

  • Instance creation expressions
  • Method invocations
  • Assignment expressions

Control statements

if
The if statement contains a test and a statement. If the test returns true, the statement block is executed.
if (username == null)
  username = "John Doe";
The if statement can also contain an else clause. If the test returns false, the first statement block is skipped over and the statement block following else is executed.
if (username == null)
  usernames += ";John Doe";
else
  usernames += ";" + username;
switch
The switch statement is used to execute a block of code among several alternatives.
switch(n):
  case 1:
    // code to be executed
  case {2, 4, 6}:
    // code to be executed
  case [10,+oo[:
    // code to be executed
  default:
    // code to be executed
Note: Unlike in Java, it is not necessary to add a break statement at the end of each case clause.
The expression after the switch can be of type:
  • Primitive
  • string
  • enum
  • Comparable
The following example shows an expression of type comparable:
BigInteger x = ...
switch(x) {
  case java.math.BigInteger.valueOf(12L):
    // code to be executed
The case expression can be of type:
  • Literal
  • List of literals in curly braces
  • Interval of literals

The following example:

switch(n):
  case 2
  case 4
  case 6:
    // code to be executed
  default:
    // code to be executed

Is equivalent to:

switch(n):
  case {2, 4, 6}:
    // code to be executed
  default:
    // code to be executed
while
The while statement contains a test and a statement. If the test evaluates to true, the statement block is executed. The while statement continues executing the statement block until the test evaluates to false.
An infinite loop can be implemented using the while statement as follows:
while (true)
  // do something
do...while
The do...while statement is close to the while statement, except that the test occurs at the bottom of the loop after the execution of the statement block.
do 
  // do something
while (false)
for
The for statement creates a loop that consists of three expressions and a statement to be executed in the loop.
for (initialization;test;increment)
  statement
  1. The initialization initializes a variable. This is executed only once.
  2. The test is evaluated. If it evaluates to true, the statement block is executed.
  3. The increment updates the value of initialization.
  4. The test is evaluated again. The process continues until test evaluates to false.
It is equivalent to:
initialization
while (test) {
  statement
  increment
}
Variables in for loops are scoped.
for (int i = 0; i < 10; i += 1)
  // do something
In this example, the variable i does not exist outside of the for loop.
for each
The for each statement is used to iterate through items of a list or an array. It uses the for keyword with the following syntax:
for (Type varname : list-or-array-expression)
  statement
For example:
List<String> list = new ArrayList<java.lang.String>();

for (String str : list) {
  // code to be executed
}
break
The break keyword can be used in while, do...while, and for statements to break out of a loop.
boolean found = false;
for (int i = 0; i < data.length; i+=1) {
  if (data[i] == target) {
    found = true;
    break;
  }
}
Note: Labeled break statements are not supported in the ARL rule language.
continue
The continue keyword can be used in while, do...while, and for statements to skip the current execution of a loop and continue with the next iteration in the loop.
for (int i = 0; i < data.length; i+=1) {
  if (data[i] == target) {
    continue;
  }
  process(data[i]);
}
return
The return keyword is used to exit from a method, with or without a value.
return x * x;
This example implements a method that computes the square of a number.
Methods of type void can

For a method declared void, the return keyword can be used without a value.