Java Reference
In-Depth Information
IF (MY-ITEMS(1) = "AA")
OR (MY-ITEMS(10) = "XX")
However, subscripts outside this range are not valid and will generate either a
compile error or a runtime error. Both of these statements will generate an error:
IF (MY-ITEMS(0) = "AA")
OR (MY-ITEMS(11) = "XX")
In contrast, Java follows the conventions of C and defines the first item in an
array as item 0. Therefore, many loops in Java start with a loop variable equal to 0
and end when that variable is equal to the number of items in the array. This means
that item[1] is actually the second item in the array, and item[ARRAY_SIZE] is not
a valid reference. Instead of the parentheses subscript identifiers that COBOL uses,
subscripts in Java are identified with brackets [ ].
T HE DO ... WHILE S TATEMENT
Structure: do { block } while (condition) ;
This loop control statement is very similar to the basic while statement, except
that block will always be executed at least once. condition is evaluated after the code
block is performed, and if true, the code block is reiterated.
// Assume errorMsgs is an array of ErrorMsg objects that has previously
// been created.
// ARRAY_SIZE is the maximum number of elements allowed in ErrorMsg.
// Examine each ErrorMsg object to find the first with a size equal to 0.
// Define x and inputMsgSize outside the while code block.
int inputMsgSize = 0;
int x = 0;
do {
inputMsgSize = errorMsgs[x].msgSize;
if (inputMsgSize == 0) {
// Exit the loop immediately. x will point to this element.
break;
Search WWH ::




Custom Search