Learning Java--- For Loop And For-Each Loop

in GEMS6 years ago

Hello Everyone!

In the last post of Java tutorials, we have discussed Loops along with while and do-while loop. As I explained in the previous post of our tutorial why the loop in programming is important and where can we use the while and do-while loop. So in the post, we will learn about for loop and for each loop and where can we use it while performing our task with Java Programming language. Let's find out what for loop is first of all

image.png
Image source

- For loop

When we know how many times we will execute the piece of code with a loop then we use for loop instead of while loop. While loop is used when we don't know the number how many times we are executing the program.

Syntax

for(init;condition;incr/decr)
{  
// code to be executed 
}

For example.

public static void main(String[] args) {
        //@pakgamer---Hive----for loop/ for-each loop

      for(int h=0;h<6;h++){
          System.out.println(h);
      }

      }
      }

Here if you focus on the first statement, We are creating an assigning a value to a variable. it is called Initialization. The second one is called condition. The compiler check the condition every time before the execution of the block of code. The third one either increases the value of the loop variable or decrease it. So in the above example will print numbers from 0-5 because the variable h value is 0 and the condition is h should be less than 6. so the compiler will increase the value by 1 until the condition is false. I hope I make some sense. Let me run the above example on the compiler.

image.png

Let's try to understand them for more. Suppose you want the table of any number. How will you do it? By multiplying one by one? but what if you need a table of 100? The line of code will reach 150+ but the for loop can do it in less than 10 lines of code. Let's see how.

  int i=5;
      for(int h=1;h<=10;h++){
          System.out.println(i+" x "+h+" = "+h*i);
      }

What exactly we are doing here is just a simple Math. We have a variable I and its value is 5 which is constant(not constant but we are not changing it) and another variable h and the value of h is increasing by 1 with the help of for loop then in the print method we are multiplying them and printing them. so at the first execution, the value of h was 1 and I was 5 so 1x5=5 then the value of h became 2 and I was still 5 so 2x5=10 and this will continue until the condition is true.

image.png

image.png

For-Each loop

The for-each loop is to access the element of an array. It is easier to use than simple for a loop because we don't need to increment value and use subscript notation.

It is based on Element not the index it returns the element of the array on by one. Let's create an array and then access it on both to understand the difference between them.


  String arr[]={"apple","mango","orange","banana"};
        for (String i: arr) {
            System.out.println(i);
        }



image.png

This is how can you access it through for-each loop and let's try the simple loop.

String arr[]={"apple","mango","orange","banana"};
        for (int i=0;i<=3;i++){
            System.out.println(arr[i]);
        }

The simple for loop access the array through the index.
image.png

ntitled-1.png

Sort:  

You post has been manually curated by BDVoter Team! To know more about us please visit our website or join our Discord.


Vote @bdcommunity as a Hive Witness.


Are you a Splinterlands player? Get instant 3% cashback on every card purchase on MonsterMarket.io. MonsterMarket has the highest revenue sharing in the space, no minimum spending is required. Join MonsterMarket Discord.

BDVoter Team