Java Reference
In-Depth Information
case 2:
print("Matched number 2: ", value);
break;
default:
print("No match:", value);
break;
}
}
// Call the match function with different arguments
match(undefined);
match(null);
match(2);
match('2');
match("Hello");
Matched undefined: undefined
Matched null: null
Matched number 2: 2
Matched string '2': 2
No match: Hello
Labelled Statements
A label is simply an identifier followed with a colon. Any statement in Nashorn can be
labelled by putting a label before the statement. In fact, Nashorn lets you have multiple
labels for a statement. Once you have labelled a statement, you can use the same label
with break and continue statements to break and continue to the label. Typically, you
label outer iteration statements so that you can continue or break out of nested loops.
Labelled statements work the same way as they do in Java. The following is an example
of using a labeled statement and a continue statement with a label to print the lower-left
half of a 3x3 matrix:
// Create a 3x3 matrix using an array of arrays
var matrix = [[11, 12, 13],
[21, 22, 23],
[31, 32, 33]];
outerLoop:
for(var i = 0; i < matrix.length; i++) {
for(var j = 0; j < matrix[i].length; j++) {
java.lang.System.out.printf("%d ", matrix[i][j]);
if (i === j) {
print();
continue outerLoop;
}
}
}
 
Search WWH ::




Custom Search