Enhanced for loop
Enhanced for loops have been available since J2SE 5.0. This type of loop uses built-in iterators over arrays
and collections to return each item in the given collection. Every element will be returned and reachable in
the context of the code block. When the block has been executed the next item will be returned until there
are no items remaining. Unlike C# this kind of loop does not involve a special keyword but instead uses a
different notation style.
for (int i : intArray) {
doSomething(i);
}
Jump statements
Labels
Labels are given points in code used by break and continue statements. Despite the presence of the goto
keyword, it cannot be used to jump to specific points in the code.
start:
someMethod();
break
It is possible to break out of the outer loop using labels:
outer:
for (int i = 0; i < 10; i++) {
while (true) {
break outer;
}
}
// Will break to this point
continue
outer:
for (String str : stringsArr) {
char[] strChars = str.toCharArray();
for (char ch : strChars) {
if (ch == ' ') {
/* Continues the outer cycle and the next
string is retrieved from stringsArr */
continue outer;
}
doSomething(ch);
}
}
Enhanced for loops have been available since J2SE 5.0. This type of loop uses built-in iterators over arrays
and collections to return each item in the given collection. Every element will be returned and reachable in
the context of the code block. When the block has been executed the next item will be returned until there
are no items remaining. Unlike C# this kind of loop does not involve a special keyword but instead uses a
different notation style.
for (int i : intArray) {
doSomething(i);
}
Jump statements
Labels
Labels are given points in code used by break and continue statements. Despite the presence of the goto
keyword, it cannot be used to jump to specific points in the code.
start:
someMethod();
break
It is possible to break out of the outer loop using labels:
outer:
for (int i = 0; i < 10; i++) {
while (true) {
break outer;
}
}
// Will break to this point
continue
outer:
for (String str : stringsArr) {
char[] strChars = str.toCharArray();
for (char ch : strChars) {
if (ch == ' ') {
/* Continues the outer cycle and the next
string is retrieved from stringsArr */
continue outer;
}
doSomething(ch);
}
}
No comments:
Post a Comment