Open In App

Understanding for loops in Java

Suppose it is required to print numbers from 1 to 5. One possible way to do this is with the help of below code:




class GFG {
    public static void main(String args[])
    {
  
        int a;
        a = 1;
        System.out.println(a);
        a=2;
        System.out.println(a);
        a=3;
        System.out.println(a);
        a=4;
        System.out.println(a);
        a=5;
        System.out.println(a);
    }
}

The above code might look simple, but it has many disadvantages, such as:

Here comes the “Loop” for help, whenever repetition of a process is required.



What are Loops?

Looping in programming languages is a feature which facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true.
Java provides three ways for executing the loops. While all the ways provide similar basic functionality, they differ in their syntax and condition checking time.

  1. while loop: A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.
    Syntax :

    while (boolean condition)
    {
       loop statements...
    }
    

    Flowchart:

  2. for loop: for loop provides a concise way of writing the loop structure. Unlike a while loop, a for statement consumes the initialization, condition and increment/decrement in one line thereby providing a shorter, easy to debug structure of looping.
    Syntax:
    for (initialization condition; testing condition; 
                                  increment/decrement)
    {
        statement(s)
    }
    

    Flowchart:

    Enhanced For loop

    Java also includes another version of for loop introduced in Java 5. Enhanced for loop provides a simpler way to iterate through the elements of a collection or array. It is inflexible and should be used only when there is a need to iterate through the elements in sequential manner without knowing the index of currently processed element.
    Syntax:

    for (T element:Collection obj/array)
    {
        statement(s)
    }
    
  3. do while: do while loop is similar to while loop with only difference that it checks for condition after executing the statements, and therefore is an example of Exit Control Loop.
    Syntax:
    do
    {
        statements..
    }
    while (condition);
    

    Flowchart:

  4. How to write a loop?

    On carefully observing the code given above, it can be broken into following processes or steps:
    1) Initialize a value to a variable
    2) Print the value
    3) Change the value
    4) Print the value

    These processes are repeated again and again. This can be replaced with a loop as follows:

Below is the execution of the required code with the help of Loop:




class GFG {
    public static void main(String args[])
    {
        int a;
        for (a = 1; a <= 5; a++) {
            System.out.println(a);
        }
    }
}


Article Tags :