0% found this document useful (0 votes)
4 views

04.Java for

This tutorial explains the Java for-each loop, which is used to iterate through elements of arrays and collections, highlighting its syntax and providing examples. It contrasts the for-each loop with the traditional for loop, demonstrating that the for-each loop is simpler and easier to understand. The document includes examples for printing array elements and calculating the sum of array elements using the for-each loop.

Uploaded by

David
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

04.Java for

This tutorial explains the Java for-each loop, which is used to iterate through elements of arrays and collections, highlighting its syntax and providing examples. It contrasts the for-each loop with the traditional for loop, demonstrating that the for-each loop is simpler and easier to understand. The document includes examples for printing array elements and calculating the sum of array elements using the for-each loop.

Uploaded by

David
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Java for-each Loop

In this tutorial, we will learn about the Java for-each loop and its difference
with for loop with the help of examples.

In Java, the for-each loop is used to iterate through elements of arrays and
collections (like ArrayList). It is also known as the enhanced for loop.

for-each Loop Sytnax


The syntax of the Java for-each loop is:

for(dataType item : array) {


...
}

Here,

• array - an array or a collection


• item - each item of array/collection is assigned to this variable
• dataType - the data type of the array/collection

Example 1: Print Array Elements


// print array elements

class Main {
public static void main(String[] args) {

// create an array
int[] numbers = {3, 9, 5, -5};
// for each loop
for (int number: numbers) {
System.out.println(number);
}
}
}
Run Code

Output

3
9
5
-5

Here, we have used the for-each loop to print each element of


the numbers array one by one.
• In the first iteration, item will be 3.
• In the second iteration, item will be 9.
• In the third iteration, item will be 5.
• In the fourth iteration, item will be -5.

Example 2: Sum of Array Elements


// Calculate the sum of all elements of an array

class Main {
public static void main(String[] args) {

// an array of numbers
int[] numbers = {3, 4, 5, -5, 0, 12};
int sum = 0;

// iterating through each element of the array


for (int number: numbers) {
sum += number;
}

System.out.println("Sum = " + sum);


}
}
Run Code

Output:

Sum = 19

In the above program, the execution of the for each loop looks as:
Iteration Variables

number = 3
1
sum = 0 + 3 = 3

number = 4
2
sum = 3 + 4 = 7

number = 5
3
sum = 7 + 5 = 12

number = -5
4
sum = 12 + (-5) = 7

number = 0
5
sum = 7 + 0 = 7

number = 12
6
sum = 7 + 12 = 19

As we can see, we have added each element of the numbers array to


the sum variable in each iteration of the loop.

for loop Vs for-each loop


Let's see how a for-each loop is different from a regular Java for loop.
1. Using for loop
class Main {
public static void main(String[] args) {
char[] vowels = {'a', 'e', 'i', 'o', 'u'};

// iterating through an array using a for loop


for (int i = 0; i < vowels.length; ++ i) {
System.out.println(vowels[i]);
}
}
}
Run Code

Output:

a
e
i
o
u

2. Using for-each Loop


class Main {
public static void main(String[] args) {

char[] vowels = {'a', 'e', 'i', 'o', 'u'};

// iterating through an array using the for-each loop


for (char item: vowels) {
System.out.println(item);
}
}
}
Run Code

Output:

a
e
i
o
u

Here, the output of both programs is the same. However, the for-each loop is
easier to write and understand.
This is why the for-each loop is preferred over the for loop when working
with arrays and collections.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy