Introduction
The enhanced for loop, also known as the for-each loop, is used to iterate over arrays and collections. It provides a more readable and concise way to traverse through elements, eliminating the need for an explicit counter or index variable. In this chapter, we will explore the syntax, usage, and examples of the for-each
loop in Java.
Syntax
The basic syntax of the for-each
loop is as follows:
for (type variable : array) {
// code to be executed
}
Key Points:
- type: The data type of the elements in the array.
- variable: A temporary variable that holds each element of the array during iteration.
- array: The array to be iterated over.
Example: Iterating Over an Array
Let’s consider an example where we use the for-each
loop to print elements of an array.
Example Code:
public class ForEachExample {
public static void main(String[] args) {
// Array of integers
int[] numbers = {1, 2, 3, 4, 5};
// Enhanced for loop to iterate over the array
for (int num : numbers) {
// Print the current element of the array
System.out.println("Number: " + num);
}
}
}
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Example: Iterating Over a String Array
Let’s consider another example where we use the for-each
loop to print elements of a string array.
Example Code:
public class ForEachStringExample {
public static void main(String[] args) {
// Array of strings
String[] fruits = {"Apple", "Banana", "Cherry", "Date"};
// Enhanced for loop to iterate over the array
for (String fruit : fruits) {
// Print the current element of the array
System.out.println("Fruit: " + fruit);
}
}
}
Output:
Fruit: Apple
Fruit: Banana
Fruit: Cherry
Fruit: Date
Example: Summing Elements in an Array
Let’s consider an example where we use the for-each
loop to calculate the sum of elements in an array.
Example Code:
public class SumForEachExample {
public static void main(String[] args) {
// Array of integers
int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;
// Enhanced for loop to iterate over the array
for (int num : numbers) {
// Add the current element to the sum
sum += num;
}
// Print the calculated sum
System.out.println("Sum: " + sum);
}
}
Output:
Sum: 15
Example: Finding the Maximum Element in an Array
Let’s consider an example where we use the for-each
loop to find the maximum element in an array.
Example Code:
public class MaxForEachExample {
public static void main(String[] args) {
// Array of integers
int[] numbers = {1, 2, 3, 4, 5};
int max = numbers[0];
// Enhanced for loop to iterate over the array
for (int num : numbers) {
// Update max if the current element is greater
if (num > max) {
max = num;
}
}
// Print the maximum element
System.out.println("Maximum: " + max);
}
}
Output:
Maximum: 5
Diagram: Flow Chart of for-each Loop
Start
|
[initialize]
|
[for-each element in array]
|
[execute loop body]
|
[next element]
|
[all elements processed]
|
End
Conclusion
The for-each
loop is a useful and concise control flow statement in Java that simplifies the process of iterating over arrays. It enhances code readability and reduces the likelihood of errors associated with traditional for
loops. By understanding the syntax and various use cases of the for-each
loop, you can write more efficient and cleaner Java code.