Showing posts with label for each loop. Show all posts
Showing posts with label for each loop. Show all posts

Jul 13, 2020

for each loop Vs for loop in JAVA | Java Language | Coding Winds

    FOR EACH LOOP


Hey guys, we are back with another blog today and in this we are going to talk about ‘for each loops’ first and then we’ll see how much it differs from an actual ‘for loop’.

For each loop:

For each loop in java in executes the statements inside the for loop according to the number of elements present inside the loop rather than pre-mentioning how long the loop will run and under what condition it will continue to.

Syntax:

for(type variableName : arrayName) {

                                                                //CODE

                                                               }

So in ‘for each loop’ we just mention the type of the value inside an array and then the variable name that we could use inside the code to play with the actual values inside the array, followed by a colon and the array name. And this loop will run until and unless the loop has run for each element inside the array. So let’s see an actual implementation of for each loop.

public class Coding_Winds {

 

     public static void main(String[] args) {

 

         String fruits[] = {"banana", "mangoes","apple", "grapes"};

         for(String x : fruits) {

              System.out.println(x);

         }

     }

}

  

    Output: banana

            mangoes

            apple

            grapes

So as we can see that in this loop we don’t care about the condition or how long the loop will run, it is designed to run for each element of the loop.

                             FOR EACH LOOP VS FOR LOOP

Now let’s see how it is different from a for loop:




Hope you are clear on this  topic do read our more articles on JAVA LANGUAGE.

If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".


This article is SUBMITTED By : Pranjal Rai


Do subscribe to our daily blog update by clicking here.


Thank You!