Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте For-Each Loop | Section
Java Fundamentals

bookFor-Each Loop

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:

Main.java

Main.java

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:

Main.java

Main.java

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); } } }

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.

Main.java

Main.java

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;.

Завдання

Swipe to start coding

Imagine you work in a warehouse, and you're given an array containing the weights of packages.
Your task is to write a program that checks if there are any packages with negative weights.

  1. Use a for-each loop to iterate through all the package weights.
  2. Inside the loop, check if the current weight is negative (< 0).
  3. If a negative weight is found, exit the loop and return false.
  4. If all values are non-negative, return true.

Рішення

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 28
single

single

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

close

bookFor-Each Loop

Свайпніть щоб показати меню

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:

Main.java

Main.java

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:

Main.java

Main.java

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); } } }

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.

Main.java

Main.java

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;.

Завдання

Swipe to start coding

Imagine you work in a warehouse, and you're given an array containing the weights of packages.
Your task is to write a program that checks if there are any packages with negative weights.

  1. Use a for-each loop to iterate through all the package weights.
  2. Inside the loop, check if the current weight is negative (< 0).
  3. If a negative weight is found, exit the loop and return false.
  4. If all values are non-negative, return true.

Рішення

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 28
single

single

some-alt