|
| 1 | +--- |
| 2 | +title: Java for-each Loop |
| 3 | +description: In this tutorial, we will learn about the Java for-each loop and its difference with for loop with the help of examples. |
| 4 | +--- |
| 5 | + |
| 6 | +In Java, the **for-each loop** is used to iterate through elements of [arrays](/docs/arrays) and collections (like [ArrayList](/docs/arraylist)). It is also known as the enhanced for loop. |
| 7 | + |
| 8 | +## `for-each` Loop Syntax |
| 9 | +The syntax of the Java **for-each** loop is: |
| 10 | + |
| 11 | +```java |
| 12 | +for(dataType item : array) { |
| 13 | + ... |
| 14 | +} |
| 15 | +``` |
| 16 | +Here, |
| 17 | + |
| 18 | +- **array** - an array or a collection |
| 19 | +- **item** - each item of array/collection is assigned to this variable |
| 20 | +- **dataType** - the data type of the array/collection |
| 21 | + |
| 22 | +### Example 1: Print Array Elements |
| 23 | + |
| 24 | +#### Input |
| 25 | + |
| 26 | +```java |
| 27 | +// print array elements |
| 28 | + |
| 29 | +class Main { |
| 30 | + public static void main(String[] args) { |
| 31 | + |
| 32 | + // create an array |
| 33 | + int[] numbers = {3, 9, 5, -5}; |
| 34 | + |
| 35 | + // for each loop |
| 36 | + for (int number: numbers) { |
| 37 | + System.out.println(number); |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | +#### Output |
| 43 | + |
| 44 | +```text |
| 45 | +3 |
| 46 | +9 |
| 47 | +5 |
| 48 | +-5 |
| 49 | +``` |
| 50 | +Here, we have used the **for-each loop** to print each element of the numbers array one by one. |
| 51 | + |
| 52 | +- In the first iteration, the item will be 3. |
| 53 | +- In the second iteration, the item will be 9. |
| 54 | +- In the third iteration, the item will be 5. |
| 55 | +- In the fourth iteration, the item will be -5. |
| 56 | + |
| 57 | +### Example 2: Sum of Array Elements |
| 58 | + |
| 59 | +#### Input |
| 60 | + |
| 61 | +```java |
| 62 | +// Calculate the sum of all elements of an array |
| 63 | + |
| 64 | +class Main { |
| 65 | + public static void main(String[] args) { |
| 66 | + |
| 67 | + // an array of numbers |
| 68 | + int[] numbers = {3, 4, 5, -5, 0, 12}; |
| 69 | + int sum = 0; |
| 70 | + |
| 71 | + // iterating through each element of the array |
| 72 | + for (int number: numbers) { |
| 73 | + sum += number; |
| 74 | + } |
| 75 | + |
| 76 | + System.out.println("Sum = " + sum); |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | +#### Output: |
| 81 | + |
| 82 | +```text |
| 83 | +Sum = 19 |
| 84 | +``` |
| 85 | + |
| 86 | +In the above program, the execution of the for-each loop looks as: |
| 87 | + |
| 88 | +| Iteration | Variables | |
| 89 | +|----------|----------| |
| 90 | +|1| `number` = 3 `sum` = 0 + 3 = 3| |
| 91 | +|2| `number` = 4 `sum` = 3 + 4 = 7 |
| 92 | +|3| `number` = 5 `sum` = 7 + 5 = 12| |
| 93 | +|4| `number` = -5 `sum` = 12 + (-5) = 7| |
| 94 | +|5| `number` = 0 `sum` = 7 + 0 = 7| |
| 95 | +|6| `number` = 12 `sum` = 7 + 12 = 19| |
| 96 | + |
| 97 | +As we can see, we have added each element of the `numbers` array to the `sum` variable in each iteration of the loop. |
| 98 | + |
| 99 | +## for loop Vs for-each loop |
| 100 | + |
| 101 | +Let's see how a for-each loop is different from a regular [Java for loop](/docs/for-loop). |
| 102 | + |
| 103 | +### 1. Using for loop |
| 104 | + |
| 105 | +#### Input |
| 106 | + |
| 107 | +```java |
| 108 | +class Main { |
| 109 | + public static void main(String[] args) { |
| 110 | + |
| 111 | + char[] vowels = {'a', 'e', 'i', 'o', 'u'}; |
| 112 | + |
| 113 | + // iterating through an array using a for loop |
| 114 | + for (int i = 0; i < vowels.length; ++ i) { |
| 115 | + System.out.println(vowels[i]); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | +#### Output: |
| 121 | + |
| 122 | +```text |
| 123 | +a |
| 124 | +e |
| 125 | +i |
| 126 | +o |
| 127 | +u |
| 128 | +``` |
| 129 | +### 2. Using for-each Loop |
| 130 | + |
| 131 | +#### Input |
| 132 | + |
| 133 | +```java |
| 134 | +class Main { |
| 135 | + public static void main(String[] args) { |
| 136 | + |
| 137 | + char[] vowels = {'a', 'e', 'i', 'o', 'u'}; |
| 138 | + |
| 139 | + // iterating through an array using the for-each loop |
| 140 | + for (char item: vowels) { |
| 141 | + System.out.println(item); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | +``` |
| 146 | +#### Output: |
| 147 | + |
| 148 | +```text |
| 149 | +a |
| 150 | +e |
| 151 | +i |
| 152 | +o |
| 153 | +u |
| 154 | +``` |
| 155 | +Here, the output of both programs is the same. However, the **for-each** loop is easier to write and understand. |
| 156 | + |
| 157 | +This is why the **for-each** loop is preferred over the **for** loop when working with arrays and collections. |
0 commit comments