More

    For loop in java: How to Outsmart Your Boss on For Loop In Java?

    The loop is used repeatedly to execute a set of statements until a particular condition is satisfied. In java we have 3 types of loop: for, while, do-while

    In this tutorial, we will learn to use the “for” loop in java

    Syntax of for loop:

    for(initialization; condition ; increment/decrement)
    {
     statement(s);
    }

    Flow of Execution of the for Loop

    As soon as a program is executed, the interpreter always monitors which statement is to be executed, we call it control flow.

    • First step : – For for loop initialization occurs first and only once, which means that the initialization part in for loop executes only once.
    • Second step : – For loop the condition is evaluated on each iteration, if the condition is true then the statements inside the body of the for loop are executed. Once the condition becomes false, the statements of the for loop are not executed and are moved to the next statement after the loop in the control program.
    • Third step : – After the execution of the statement inside the body of the for loop, the increment / decrement part of the for loop is executed which updates the counter of the loop.
    • Fourth step : – After the third step, the control moves to the second stage and the condition is evaluated again.
    class ForLoopExample {
     public static void main(String args[]){
     for(int i=10; i>1; i--){
     System.out.println("The value of i is: "+i);
     }
     }
    }

    Output: –
    The value of i is: 10
    The value of i is: 9
    The value of i is: 8
    The value of i is: 7
    The value of i is: 6
    The value of i is: 5
    The value of i is: 4
    The value of i is: 3
    The value of i is: 2

    In the above program

    int i = 1 is the initialization expression
    i> 1 is the condition
    iā€“, Decrement operation

    Infinite for loop

    Importance of coordinating Boolean expression and increment / decrement operation

    class ForLoopExample2 {
     public static void main(String args[]){
     for(int i=1; i>=1; i++){
     System.out.println("The value of i is: "+i);
     }
     }
    }

    This is an infinite loop because the condition here will never be false, the initialization step variable is setting a value (i) of 1 to i, because we are increasing the value of i in the condition here. And it will always be greater than 1 (Boolean expression: i> 1), so it will never be false. This will generate an infinite loop condition. Therefore it is important to have coordination between boolean expression and increment / decrement.

    Switch case statement in java: How to use Switch case?

    Here is another example for infinite for loop

    // infinite loop
    for (; šŸ˜‰ {
     // statement(s)
    }

    For loop example to iterate an array to iterate over an array.

    class ForLoopExample3 {
     public static void main(String args[]){
     int arr[]={2,11,45,9};
     //i starts with 0 as array index starts with 0 too
     for(int i=0; i<arr.length; i++){
     System.out.println(arr[i]);
     }
     }
    }

    Output: –
    2
    11
    45
    9

    Enhanced for loop

    Enhanced for loop is necessary when you want to iterate over array / collections, it is easy to write and understand. The above example is written once again using Enhanced for loop –

    class ForLoopExample3 {
     public static void main(String args[]){
     int arr[]={2,11,45,9};
     for (int num : arr) {
     System.out.println(num);
     }
     }
    }

    Output:
    2
    11
    45
    9

    Note: – In the above example, variable num is declared as Enhanced for loop giving the form of integer data type, it can be changed according to the data type of the array, for example Enhanced for loop for string data type would be like this. –

    String arr[]={"hi","hello","bye"};
    for (String str : arr) {
     System.out.println(str);
    }

    Recent Articles

    Related Stories

    Leave A Reply

    Please enter your comment!
    Please enter your name here

    Stay on op - Ge the daily news in your inbox