Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
For-Each Loop | Arrays
Java Basics
course content

Course Content

Java Basics

Java Basics

1. Getting Started
2. Basic Types, Operations
3. Loops
4. Arrays
5. String

bookFor-Each Loop

You might wonder why we cover this loop in the section about arrays. That's because this loop is specifically designed for use with arrays or collections. You'll delve deeper into collections in a separate course.

What is a for-each loop?

The for-each loop, also referred to as the enhanced for loop, offers a concise and simplified method for iterating over elements of an array or any iterable collection in Java. It removes the necessity for explicit indexing and streamlines the code.

The syntax for this loop is as follows:

java

Main

copy
123
for (ElementType variable : iterable) { // Code to be executed for each element }

Explanation of each element:

  • ElementType: The data type of elements in the array;
  • variable: A variable representing each array element in each iteration;
  • iterable: The array or iterable collection you want to iterate over.

The for-each loop automatically iterates over each element of the array or iterable collection, assigning it to the variable in each iteration until all elements have been processed.

Let's take a look at an example of using a for-each loop:

java

Main

copy
1234567891011
package com.example; public class Main { public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5}; for (int element : array) { System.out.println(element); } } }

Note

We are not using an index here. To perform operations on an array element, we refer to it as "element" rather than "array[element]".

We can also perform operations on each element of an array using a for-each loop. Let's consider an example task:

You need to multiply each element of a double array by 3 and display the result on the screen using a for-each loop.

java

Main

copy
123456789101112
package com.example; public class Main { public static void main(String[] args) { double[] array = {1.5, 2.0, 3.4, 4.5, 5.0}; for (double element : array) { element = element * 3; System.out.println(element); } } }

As you can see, we are multiplying each element by 3 without using indexing, using the statement element = element * 3;.

Task
test

Swipe to show code editor

We have a one-dimensional array of type int where all the values are either 5 or 7. However, I don't like the number 7; it's just not aesthetically pleasing. Using a for-each loop, replace every occurrence of the number 7 in this array with the number 5 so that it becomes an array of 5s. Don't forget to print the resulting array.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 6
toggle bottom row

bookFor-Each Loop

You might wonder why we cover this loop in the section about arrays. That's because this loop is specifically designed for use with arrays or collections. You'll delve deeper into collections in a separate course.

What is a for-each loop?

The for-each loop, also referred to as the enhanced for loop, offers a concise and simplified method for iterating over elements of an array or any iterable collection in Java. It removes the necessity for explicit indexing and streamlines the code.

The syntax for this loop is as follows:

java

Main

copy
123
for (ElementType variable : iterable) { // Code to be executed for each element }

Explanation of each element:

  • ElementType: The data type of elements in the array;
  • variable: A variable representing each array element in each iteration;
  • iterable: The array or iterable collection you want to iterate over.

The for-each loop automatically iterates over each element of the array or iterable collection, assigning it to the variable in each iteration until all elements have been processed.

Let's take a look at an example of using a for-each loop:

java

Main

copy
1234567891011
package com.example; public class Main { public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5}; for (int element : array) { System.out.println(element); } } }

Note

We are not using an index here. To perform operations on an array element, we refer to it as "element" rather than "array[element]".

We can also perform operations on each element of an array using a for-each loop. Let's consider an example task:

You need to multiply each element of a double array by 3 and display the result on the screen using a for-each loop.

java

Main

copy
123456789101112
package com.example; public class Main { public static void main(String[] args) { double[] array = {1.5, 2.0, 3.4, 4.5, 5.0}; for (double element : array) { element = element * 3; System.out.println(element); } } }

As you can see, we are multiplying each element by 3 without using indexing, using the statement element = element * 3;.

Task
test

Swipe to show code editor

We have a one-dimensional array of type int where all the values are either 5 or 7. However, I don't like the number 7; it's just not aesthetically pleasing. Using a for-each loop, replace every occurrence of the number 7 in this array with the number 5 so that it becomes an array of 5s. Don't forget to print the resulting array.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 6
toggle bottom row

bookFor-Each Loop

You might wonder why we cover this loop in the section about arrays. That's because this loop is specifically designed for use with arrays or collections. You'll delve deeper into collections in a separate course.

What is a for-each loop?

The for-each loop, also referred to as the enhanced for loop, offers a concise and simplified method for iterating over elements of an array or any iterable collection in Java. It removes the necessity for explicit indexing and streamlines the code.

The syntax for this loop is as follows:

java

Main

copy
123
for (ElementType variable : iterable) { // Code to be executed for each element }

Explanation of each element:

  • ElementType: The data type of elements in the array;
  • variable: A variable representing each array element in each iteration;
  • iterable: The array or iterable collection you want to iterate over.

The for-each loop automatically iterates over each element of the array or iterable collection, assigning it to the variable in each iteration until all elements have been processed.

Let's take a look at an example of using a for-each loop:

java

Main

copy
1234567891011
package com.example; public class Main { public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5}; for (int element : array) { System.out.println(element); } } }

Note

We are not using an index here. To perform operations on an array element, we refer to it as "element" rather than "array[element]".

We can also perform operations on each element of an array using a for-each loop. Let's consider an example task:

You need to multiply each element of a double array by 3 and display the result on the screen using a for-each loop.

java

Main

copy
123456789101112
package com.example; public class Main { public static void main(String[] args) { double[] array = {1.5, 2.0, 3.4, 4.5, 5.0}; for (double element : array) { element = element * 3; System.out.println(element); } } }

As you can see, we are multiplying each element by 3 without using indexing, using the statement element = element * 3;.

Task
test

Swipe to show code editor

We have a one-dimensional array of type int where all the values are either 5 or 7. However, I don't like the number 7; it's just not aesthetically pleasing. Using a for-each loop, replace every occurrence of the number 7 in this array with the number 5 so that it becomes an array of 5s. Don't forget to print the resulting array.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

You might wonder why we cover this loop in the section about arrays. That's because this loop is specifically designed for use with arrays or collections. You'll delve deeper into collections in a separate course.

What is a for-each loop?

The for-each loop, also referred to as the enhanced for loop, offers a concise and simplified method for iterating over elements of an array or any iterable collection in Java. It removes the necessity for explicit indexing and streamlines the code.

The syntax for this loop is as follows:

java

Main

copy
123
for (ElementType variable : iterable) { // Code to be executed for each element }

Explanation of each element:

  • ElementType: The data type of elements in the array;
  • variable: A variable representing each array element in each iteration;
  • iterable: The array or iterable collection you want to iterate over.

The for-each loop automatically iterates over each element of the array or iterable collection, assigning it to the variable in each iteration until all elements have been processed.

Let's take a look at an example of using a for-each loop:

java

Main

copy
1234567891011
package com.example; public class Main { public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5}; for (int element : array) { System.out.println(element); } } }

Note

We are not using an index here. To perform operations on an array element, we refer to it as "element" rather than "array[element]".

We can also perform operations on each element of an array using a for-each loop. Let's consider an example task:

You need to multiply each element of a double array by 3 and display the result on the screen using a for-each loop.

java

Main

copy
123456789101112
package com.example; public class Main { public static void main(String[] args) { double[] array = {1.5, 2.0, 3.4, 4.5, 5.0}; for (double element : array) { element = element * 3; System.out.println(element); } } }

As you can see, we are multiplying each element by 3 without using indexing, using the statement element = element * 3;.

Task
test

Swipe to show code editor

We have a one-dimensional array of type int where all the values are either 5 or 7. However, I don't like the number 7; it's just not aesthetically pleasing. Using a for-each loop, replace every occurrence of the number 7 in this array with the number 5 so that it becomes an array of 5s. Don't forget to print the resulting array.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 4. Chapter 6
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt