Tuesday 28 June 2016

For Loop in Java

Java For Loop

The Java for loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop.
There are three types of for loop in java.
  • Simple For Loop
  • For-each or Enhanced For Loop
  • Labeled For Loop
Here is the flow of control in a for loop:
  • The initialization step is executed first, and only once. This step allows you to declare and initialize any loop control variables. and this step ends with a semi colon (;)
  • Next, the Boolean expression is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop will not be executed and control jumps to the next statement past the for loop.
  • After the body of the for loop gets executed, the control jumps back up to the update statement. This statement allows you to update any loop control variables. This statement can be left blank with a semicolon at the end.
  • The Boolean expression is now evaluated again. If it is true, the loop executes and the process repeats (body of loop, then update step, then Boolean expression). After the Boolean expression is false, the for loop terminates.

     


 

Java Simple For Loop

The simple for loop is same as C/C++. We can initialize variable, check condition and increment/decrement value.
Syntax:
  1. for(initialization;condition;incr/decr){  
  2. //code to be executed  
  3. }  
for loop in java flowchart
Example:
  1. public class ForExample {  
  2. public static void main(String[] args) {  
  3.     for(int i=1;i<=10;i++){  
  4.         System.out.println(i);  
  5.     }  
  6. }  
  7. }  
Output:
1
2
3
4
5
6
7
8
9
10

Java For-each Loop

The for-each loop is used to traverse array or collection in java. It is easier to use than simple for loop because we don't need to increment value and use subscript notation.
It works on elements basis not index. It returns element one by one in the defined variable.
Syntax:
  1. for(Type var:array){  
  2. //code to be executed  
  3. }  
Example:
  1. public class ForEachExample {  
  2. public static void main(String[] args) {  
  3.     int arr[]={12,23,44,56,78};  
  4.     for(int i:arr){  
  5.         System.out.println(i);  
  6.     }  
  7. }  
  8. }  
Output:
12
23
44
56
78

Java Labeled For Loop

We can have name of each for loop. To do so, we use label before the for loop. It is useful if we have nested for loop so that we can break/continue specific for loop.
Normally, break and continue keywords breaks/continues the inner most for loop only.
Syntax:
  1. labelname:  
  2. for(initialization;condition;incr/decr){  
  3. //code to be executed  
  4. }  
Example:
  1. public class LabeledForExample {  
  2. public static void main(String[] args) {  
  3.     aa:  
  4.         for(int i=1;i<=3;i++){  
  5.             bb:  
  6.                 for(int j=1;j<=3;j++){  
  7.                     if(i==2&&j==2){  
  8.                         break aa;  
  9.                     }  
  10.                     System.out.println(i+" "+j);  
  11.                 }  
  12.         }  
  13. }  
  14. }  
Output:
1 1
1 2
1 3
2 1
If you use break bb;, it will break inner loop only which is the default behavior of any loop.
  1. public class LabeledForExample {  
  2. public static void main(String[] args) {  
  3.     aa:  
  4.         for(int i=1;i<=3;i++){  
  5.             bb:  
  6.                 for(int j=1;j<=3;j++){  
  7.                     if(i==2&&j==2){  
  8.                         break bb;  
  9.                     }  
  10.                     System.out.println(i+" "+j);  
  11.                 }  
  12.         }  
  13. }  
  14. }  
Output:
1 1
1 2
1 3
2 1
3 1
3 2
3 3

Java Infinitive For Loop

If you use two semicolons ;; in the for loop, it will be infinitive for loop.
Syntax:
  1. for(;;){  
  2. //code to be executed  
  3. }  
Example:
  1. public class ForExample {  
  2. public static void main(String[] args) {  
  3.     for(;;){  
  4.         System.out.println("infinitive loop");  
  5.     }  
  6. }  
  7. }  
Output:
infinitive loop
infinitive loop
infinitive loop
infinitive loop
infinitive loop
ctrl+c
Now, you need to press ctrl+c to exit from the program.

No comments:

Post a Comment