Enhanced for Loop (for-each)

The enhanced for loop (also called for-each loop) is used to iterate through arrays or collections in a simpler and cleaner way — without using an index.

Key Points :

  1. Used to traverse arrays or collections easily.
  2. No need to manage index or loop counters.
  3. Automatically picks each element one by one.
  4. Read-only loop — you can access, but not change the original array values directly.
  5. Best for reading values, not for updating them.

Syntax:

for (datatype var : array) {
    // code using var
}
  • for (datatype var : array) means take each element from the array one by one.
  • var holds the current element in each iteration.
  • The loop runs automatically for each value, without using index or counters.

Example:

class ShikshaSanchar {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40, 50};
        
        for (int num : numbers) {
            System.out.println("Number: " + num);
        }
    }
}

Output:

Number: 10

Number: 20

Number: 30

Number: 40

Number: 50

Explanation :

  1. int[] numbers is an array of integers.
  2. The enhanced for loop goes through each number in the array.
  3. On each iteration, num holds the current value from the array.
  4. The loop prints each number one by one.
  5. No need to write numbers[i] or manage i++.

Flowchart : for-each Loop in Java

Java for-each loop Diagram

This diagram visually shows how a for-each loop works — where each element of the array/collection is picked automatically.

  1. Start
    • The program begins execution.
    • Control moves to array declaration.
  2. Declare Array
    • int[] numbers = {10, 20, 30, 40, 50};
    • An array of integers is declared.
    • This step sets up the values the loop will iterate over.
  3. Loop Begins
    • for (int num : numbers)
    • The enhanced for loop starts.
    • It automatically picks the first element (10) from the array.
  4. Print Statement
    • System.out.println("Number: " + num);
    • Prints the current value of num.
    • First time: prints Number: 10, then Number: 20, and so on...
  5. Next Element?
    • Checks: Are there more elements left in the array?
    • If Yes Loop goes back to pick the next element → Step 3.
    • If No All elements are done → move to End.
  6. End
    • Loop is finished.
    • The program ends after printing all values from the array.

When to Use:

  • When you just want to read or display elements.
  • When you don’t need the index of elements.
  • Perfect for arrays, ArrayList, HashSet, etc.

Limitations:

  • You can’t modify array elements directly.
  • You can’t access index inside the loop.

Summary:

  1. Simplifies array/collection traversal.
  2. No index or counter is needed.
  3. Easy to read and write.
  4. Best for read-only tasks.
  5. Can’t access index or modify elements directly.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar