Looping Constructs in Java For and For Each
Looping Constructs in Java For and For Each
FOR-EACH
Looping constructs in Java are:
For statement
For-each statement
While statement
Do while statement
We will see for and for-each here. We will see while, do while, break and continue statements in next
note.
If there are more than one statement in the body, it needs to be enclosed in braces ({}). If there is
only one statement, braces are optional, but is preferred for readability. We can also have an empty
statement (;) as the body.
The initialization-code part is executed only once in the beginning, terminal-condition part id checked
every time before executing the body and will execute only if it return true, and increment-decrement
part is executed every time after executing the body. The increment-decrement part can increment,
decrement or otherwise modify the variable as needed.
System.out.print(i);
First int i=0 will be executed. Then i<5 is evaluated and since it return true, the body is executed.
Once the body is executed, i++ is executed. Again i<5 is evaluated, body is executed and i++ is
executed. Once i becomes 5, the execution will come out of the loop. Above statement will print:
01234
You can use comma operator to separate statements within initialization-code part and increment-
decrement, but not in the terminal-condition.
System.out.print(i+j);
If you use comma operator in the condition part, you will get compilation error. You need to use a
valid logical expression, a simple one like above or a complex one like (i<5) && (j>3).
The index variable used by a for statement can have different scope depending on how it is
declared. If it is declared within the initialization-code part of the for loop (as in the above example),
its scope and lifetime will be limited to the for loop block and any inner blocks. Instead if we have
declared it outside, its scope and life time will be that of the outer block.
System.out.print(i+j);
System.out.print(i+j);
For a local variable, scope and lifetime is limited to that block and any of its children. For more
details on variables and scopes, refer to javajee.com/data-types-and-variables-in-java.
The initial operation, terminal condition, or end loop operation are not required, but optional in a for
loop. We can also have an empty statement (;) as the body. Below code is valid, but creates an
infinite loop:
for(;;)
;
The for-each statement
The for-each statement aka the enhanced for loop, provides an easier way to iterate through an
array or a class that has implemented the java.util.Iterable interface. This interface consists of a
single method, iterator that returns an Iterator. The Iterable interface is the super interface of the
java.util.Collection interface, and hence the for-each statement is usually used with arrays and
classes that implement the Collection interface like List, Set and Map implementations.
Syntax is:
Note that even though this is called a for-each loop, there is no each keyword.
list.add("Heartin");
list.add("Sneha");
list.add("June");
System.out.println(str);
4. You can't modify (add or remove elements) a collection you are iterating over. If you try to modify like
below, you will get a java.util.ConcurrentModificationException.
if(str.equals("abc"))
list.remove("abc");
int[] arr={1,2,3,4,5};
System.out.print(val);
arr[2]=7;
However this doesn't make much sense as we need to use the array Index here which is not
available with a for-each loop.
Source : http://javajee.com/looping-constructs-in-java-for-and-for-each