Branching statements
break and continue branching statements in
actions.
IRL supports two branching statements: the break statement and the
continue statement.
Break
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 exists. 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
Continue
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 runs the next
iteration of the loop. The continue statement is
demonstrated in the following example.
then {
StringBuffer ?whitePaper = new StringBuffer(
"The Case for Business Users of Information Technology");
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 6 s's in the string. The CaSe for BuSineSS USerS of Information Technology
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.