Java For Loop

Java For Loop

In Java, for loop is used to iterate over a set of statements that need to be executed a certain number of times. It is preferred to use when the number of iterations is known prior to the programmer.

Syntax of for loop:

for(initialization(s);condition(s);update_expression(s)){
//for-loop body
}

Parts of For Loop:

  • For loop consists of 3 parts which are separated from each other using semicolon:
  • initialization: Loop variable(s) is initialized in this part and is executed only once.
  • condition: Condition(s) is stated in this section and is executed at the beginning of every iteration.
  • update_expression: Update(s) is performed in this section and is executed at the end of every iteration.

Flowchart of for Loop:

flowchart of for loop in java

 

Java For-loop Example:

//Java program with for loop
public class ForLoop {
    public static void main(String[] args){
        System.out.println("Numbers from 1 to 10 that are divisible by 3.");
        for(int i=1;i<=10;i++){
            if(i%3==0){
                //executes when if-condition is true
                System.out.println(i);
            }
        }
    }
}

The above program simply prints numbers from 1 to 10 on separate lines if they are divisible by 3.

Output:

Numbers from 1 to 10 that are divisible by 3.
3
6
9

 

 

Java Nested For Loop

If a for loop has another for loop inside it, then it is a nested loop. For every iteration of the outer loop, the inner loop executes completely.

Syntax:

for(initialization(s);condition(s);update_expression(s)){
//loop body
    for(initialization(s);condition(s);update_expression(s)){
        //loop body
    }
}

Nested For-Loop Example:

//Java program with nested for loop
public class NestedForLoop {
    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();
        }
    }
}

In the program provided above, nested for loop has been used to print a pyramid of stars to the console in the shape of a right-angled triangle.

Output:

*
**
***
****
*****

 

 

Java For each Loop

For Each loop is used to traverse an array or collection in java. A user-defined variable is created to which the current element is assigned in each iteration and the array or collection pointer is moved to the next element in the array or collection which is processed in the next iteration.

Note:

It works on an element basis, not on an index basis.

Syntax:

for(variable: array/collection){
  //loop body
}

For-each Loop Example:

//Java program with for-each loop
public class ForEachLoop {
    public static void main(String[] args){
        //array creation
        int arr[]={1,2,3,4,5};
        System.out.println("Elements of arr[] are: ");
        
        //Traversing array using for-each loop
        for(int i:arr){
            System.out.println(i);
        }
    }
}

In the program provided above, an array “arr” has been created of type int with 5 elements. Those elements are then printed on the console using for-each loop which traverses each element of the array one-by-one and prints it.

Output:

Elements of arr[] are: 
1
2
3
4
5

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *