Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Steps 
2.1.
Pseudo Code
2.2.
Foreach loop in Java
2.2.1.
Output
2.3.
Foreach loop in C++
2.3.1.
Output
3.
Advantages
4.
Frequently asked questions
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

For Each Loop

Author Amarjeet kumar
6 upvotes

Introduction

  • In most cases, for-each is used instead of a typical for loop statement. Foreach loops, unlike other for loop structures, usually don't have an explicit counter: they essentially state "do this to everything in this collection" rather than "do this x times." It prevents off-by-one problems and makes the code easier to read. An Iterator is frequently used as a traversal mechanism in object-oriented languages, even if implicit. You can explore more about different types of loops.
     
  • The for-each loop is used to quickly access items of an array without having to do initialization, testing, or increment/decrement operations. Foreach loops work by doing something for each element instead of doing something n times.

Steps 

  • For using for each loop, we need a loop to iterate through.
  • Write the syntax for the for each loop for the programming language.
  • Initialize a variable and reference it to the object which is neet to iterate. 

Pseudo Code

array a[] = [elements]
n = size(a)
for each i in a from 0 to n-1:
    print a[i]

Foreach loop in Java

int[] arr ={1,2,3,4,5,6,7,8,9,0};
        
        // for each loop
        for (int num : arr)
        {
          System.out.println(num);
        }
You can also try this code with Online Java Compiler
Run Code

Output

1
2
3
4
5
6
7
8
9
0
You can also try this code with Online C++ Compiler
Run Code

Know more about What are Loops in Java and  Rabin Karp Algorithm


Foreach loop in C++

int main()
{
    int arr[] = {1,2,3,4,5,6,7,8,9,0};
  
    // Printing elements of an array using
    // foreach loop
    for (int num : arr)
        cout << num << endl;
}
You can also try this code with Online C++ Compiler
Run Code

Output

1
2
3
4
5
6
7
8
9
0
You can also try this code with Online C++ Compiler
Run Code

Advantages

  • Since you don't have to dereference inside the loop, for each is convenient. You never have to worry about over-or under-running your data.
     
  • It also gives the compiler a little more information about what you're doing, leading to more optimizations.
     
  • It makes the code easier to read and reduces the likelihood of programming errors. Check sample coding problems solve using loops.

 

Must Read:  Java List Iterator

Frequently asked questions

  1. Is it advisable to use it for each loop instead of for the loop?
    As the local variable that stores the value of an array element is faster to access than an array element, this for each loop is faster. If the array is only accessed once each iteration, the for loop is faster than the for each loop.
     
  2. What is the purpose of a For Each loop?
    A for-each loop is a loop that can only be applied to a group of elements. It will loop through the collection, using the following item from the display each time it loops through. It begins with the first item in the array (at index 0) and proceeds through the array to the last item.
     
  3. What is forEach Python?
    In Python, there is no such thing as a for each statement. The language has loops built in. for the Iterable element: operate(element). You could write for each method if you wanted to.
     
  4. What is the distinction between Iterator and ListIterator?
    Iterator can only traverse in one direction, whereas ListIterator can traverse forward and backward. ListIterator, unlike Iterator, can assist in the replacement of an element.
     
  5. Is the for-each loop supported in C?
    In C, there is no for each. You can loop through the data with a for loop, but the length must be known, or a known value must terminate the data (e.g., null).

Conclusion

This article has gone through an important topic for each loop. The for-each loop can remove the potential for problems and make the code more readable. The for-each loop gets its name because it goes over each element. The improved for loop has the disadvantage of not being able to explore the components in reverse order. Want to learn more about loops click here. 

Recommended Readings:

Attempt our Online Mock Test Series on Coding Ninjas Studio now!
 

Ninja, have a great time learning.

Live masterclass