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
ifstatement contains a test and a statement. If the test returnstrue, the statement block is executed.if (username == null) username = "John Doe"; switch- The
switchstatement 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 executedNote: Unlike in Java, it is not necessary to add abreakstatement at the end of eachcaseclause. while- The
whilestatement contains a test and a statement. If the test evaluates totrue, the statement block is executed. Thewhilestatement continues executing the statement block until the test evaluates tofalse. do...while- The
do...whilestatement is close to thewhilestatement, 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
forstatement creates a loop that consists of three expressions and a statement to be executed in the loop.for (initialization;test;increment) statement- The
initializationinitializes a variable. This is executed only once. - The
testis evaluated. If it evaluates totrue, the statement block is executed. - The
incrementupdates the value ofinitialization. - The
testis evaluated again. The process continues untiltestevaluates tofalse.
- The
for each- The
for eachstatement is used to iterate through items of a list or an array. It uses theforkeyword with the following syntax:
For example:for (Type varname : list-or-array-expression) statementList<String> list = new ArrayList<java.lang.String>(); for (String str : list) { // code to be executed } break- The
breakkeyword can be used inwhile,do...while, andforstatements 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: Labeledbreakstatements are not supported in the ARL rule language. continue- The
continuekeyword can be used inwhile,do...while, andforstatements 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
returnkeyword is used to exit from a method, with or without a value.
This example implements a method that computes the square of a number.return x * x;