Tuesday, January 14, 2020

Java for statement

The for statement


The for-loop is an iteration statement like the while and do-while. The for-statement is the most popular iteration statement among programmers. When we need to execute any part of the program repeatedly until a predefined condition is met, we can use for-loop. The for-loop is a tool used to loop over a range of values from beginning to end.



Syntax:

Simple for-loop

for ( initialization; continuation-condition; update )
     do-statement


or



for ( initialization; continuation-condition; update ) {
     do-statements block
}



Initialization

Initialization is the first expression that occurs within for loop parenthesis, this expression is executed once at the starting of for loop. Generally, this a counter variable that can be tested against some condition and updated later.

Condition

Condition is a boolean expression, and it is executed every time the loop is iterated, only if this condition holds true then only the control of execution will move ahead, otherwise, the loop will halt.

Update

An update is an expression, that occurs before the next evaluation of the condition, Generally, this is an update of the counter declared in the initialization step.

Statement block


Statement block can carry single or multiple statements to be executed repeatedly over and over until the condition is failed. Generally, this block is kept within {......} brackets if we have multiple statements in the statement block. Otherwise, only the first statement will be iterated by the for-loop. 




There are three versions of for loop in Java

  • simple for loop
  • for-each or enhanced for loop
  • labeled for loop

Enhanced for loop:

The enhanced for-statements were introduced in J2SE 5.0. The enhanced for-loop provides an alternative approach to traverse the array or collection in Java. The enhanced for-loops are used to traverse the array or collection elements mainly. The enhanced for-loop are also known as the for-each loop because each item is traversed one by one.


Nested for-loop


We can use for loop in nested formation as shown in following example.

public class Test {
     public static void main(String[] args) {
             for(int i=0;i<5;i++)
             {
               for(int j=0;j<=i;j++)
               {
                 System.out.print("+");
               }
               System.out.println();
             }
     }
}

Output:
+
++
+++
++++
+++++

Labeled for loop:

We can provide a label to a for-loop to identify uniquely when multiple loops are used in nested formation. Labeling is generally used with break and continue statements.

public class Test {
     public static void main(String[] args) {
             for(int i=0;i<5;i++)
             {
              lbl:for(;;) //labeled for loop
              {
               for(int j=0;j<=i;j++)
               {
                 System.out.print("+");
                 if(j%2==0)
                  break lbl;
               }
               System.out.println();
              }
             }
     }
} 


Output:
+++++

Video Tutorial