| ILOG JRules User Guide > Writing Rules > Concepts > Technical Rules > Technical Rule Actions > Branching Statements |
Branching Statements |
PREVIOUS NEXT |
IRL supports two branching statements: the break statement and the continue statement.
The break statement terminates a for or a while loop when the statement is found. In a for loop, for example, the flow of control transfers to the statement following the enclosing for, as shown here:
then {
int ?i = 1;
int ?j = 1;
for ( ?i = 1; ?i <= 3; ?i++ ) {
System.out.println("\ni= " + ?i + ": ");
for ( ?j = 1; ?j <= 3; ?j++ ) {
System.out.println(" j = " + ?j) ;
if ( ?i == ?j ) {
break;
}
}
}
}
The break statement ends only the loop in which it appears. If two loops are nested, a break in the inner loop exits the inner loop but not the outer loop. The output of the above example is shown here:
i= 1: j = 1 i= 2: j = 1 j = 2 i= 3: j = 1 j = 2 j = 3
You use the continue statement to skip the current iteration of a for or while loop. Instead of ending the loop like the break statement, the continue statement skips all following statements in the loop body and triggers the next iteration of the loop. The continue statement is demonstrated in the following example.
then {
StringBuffer ?whitePaper = new StringBuffer(
"Business Rules, Powering Business and e-Business");
int ?max = ?whitePaper.length();
int ?numSs = 0;
int ?i = 0;
for (?i = 0; ?i < ?max; ?i++) {
if (?whitePaper.charAt(?i) != 's'){
continue;
}
?numSs++;
?whitePaper.setCharAt(?i, 'S');
}
System.out.println("Found " + ?numSs + " s's in the string.\n");
System.out.println(?whitePaper);
}
Here is the output:
Found 10 s's in the string. BuSineSS RuleS, Powering BuSineSS and e-BuSineSS
The example steps through a string buffer checking each letter. If the current character is not an s, the continue statement skips the rest of the statements in the loop and proceeds to the next iteration to test the next character. If it is an s, the rule increments a counter and converts the s to uppercase.
| Copyright © 1987-2008 ILOG S.A. All rights reserved. Legal terms. Documentation homepage. | PREVIOUS NEXT |