Professional Documents
Culture Documents
LOOPS – REVIEW
ABBREVIATIONS & ACRONYMS
The standard for loop is the most basic and versatile loop of all; it can replace any other
loop construct.
do-while executes at least once.
The break statement breaks out of a loop ‛cleanly’, in other words, by fully abandoning
current iteration and any other remaining iterations of the loop. Control is transferred to
the first statement after the loop.
When the loop for(i = 0; i < n; i++) terminates, the value of i is n and not n-1.
Nested loops can appear intimidating but chances are the flow won’t even enter some of
those damned loops.
The weirder the loop the higher the chances the code is booby-trapped
somewhere else and, what’s more, on a pretty simple aspect.
5
WORKING WITH LOOPS, cont’d
If your analysis shows that the flow does enter a loop, write down each
iteration with all its steps; cracking loops mentally at home is cool but probably
not a good idea on the exam.
Vertical notation works better than writing the results of each iteration in rows
because it’s easier to correct values this way or start over from a particular
point if you lose concentration for a sec:
6
WORKING WITH LOOPS, cont’d
The rule sounds pretty obvious but endless loops are tricky: the code will throw
the "unreachable statement" comperr only if
1) the boolean expression can be evaluated at compile time, and
2) there’s no escape from the loop.
The curly braces {} are NOT required for loops → be careful: the code’s
business logic can be in jeopardy.
continue is valid only inside loops; ANY loop will do including for-each,
do-while and while. 8
WORKING WITH LOOPS, cont’d
Labels:
can be added to:
o a sub-block;
o any looping construct such as for, for-each, while, or do-while;
o conditional constructs such as if and switch;
o expressions and assignments;
o return stats;
o TCF constructs, and
o throw stat.
cannot be added to declarations of variables.
Although labeled break is commonly used with loops, it is still valid outside them
(e.g., in the if constructs, etc.). Watch out, however, for its scope as it might lead to
the ‛unreachable statement’ comperr.
9
WORKING WITH LOOPS, cont’d
Labeled break needs a block or an if with at least a single break label; stat in it:
class Label {
String label(){
// label int a = 10; // INVALID
int a = 10;
// label: if (true) break; // INVALID
label: if (true) break label;
label: for (;true;) break label;
label: switch(a) {}
label: a = 42;
label: a++;
label: try{ label2: throw new NullPointerException(); }
catch(NullPointerException npe){ break label; }
label: return "Label!";
}
}
As labels bind to either loops or blocks, outside them they are out of scope →
comperr.
Corollary: If there's a labeled continue it must always be inside the loop the label is
declared for.
10
WORKING WITH LOOPS, cont’d
The for-each loop is routinely used to iterate over Collections such as Lists.
However, it cannot be used to:
initialize or modify the elements of an array;
delete the elements of a Collection;
iterate over multiple collections or arrays within the same loop.
11
WORKING WITH LOOPS, cont’d
12
WORKING WITH LOOPS, cont’d
EXTRA: Labels are ‛recyclable’ (because they go out of scope as soon as the block or
construct they bind to is over):
public static void main(String[] args){ // outputs 1234
label: {
System.out.print("1");
if (false) break label; System.out.print("2"); }
label:
if (1 > 0) {
System.out.print("3");
if (false) break label; System.out.print("4"); }}
EXTRA: Booby-trapped patterns:
i = i++; never increments. (See the discussion on Stack Overflow )
a break-continue fork in ANY loop makes the next stat inside the loop unreachable:
for(int i = 0; i<10; i++){
if(i == 3){ break;
} else { continue; }
// System.out.println("!"); // unreachable stat → comperr
}
for(...;...;<no update>){<no update>} creates a valid, endless loop. 14