Skip to content

Commit f832352

Browse files
authored
Update 2021-12-06-for-each-loop.md
1 parent c8237b6 commit f832352

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

‎_posts/java-basic/2021-12-06-for-each-loop.md

+12-14
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ title: "For-each loop in Java or Enhanced For loop in Java"
44
author: gaurav
55
categories: [Java, Control Flow Statements]
66
toc: true
7-
description: "In this article, we will see the for-each loop in java i.e. Enhanced for loop"
7+
description: "In this article, we will see the for-each loop in Java i.e. Enhanced for loop"
88
---
9-
In this article, we will see the for-each loop in java i.e. Enhanced for loop
9+
In this article, we will see the for-each loop in Java i.e. Enhanced for-loop
1010

1111
## Introduction
1212

1313
For-each loop is introduced in Java 1.5.
1414

1515
It provides a better approach to traversing the array or collection.
1616

17-
In a simple for loop, we initialize the variable and write the condition for that variable. But in for each loop, we do not have to initialize a variable or write any condition for that.
17+
In a simple for loop, we initialize the variable and write the condition for that variable. But in the enhanced loop, we do not have to initialize a variable or write any condition for that.
1818

1919
The foreach loop is always used to traverse over the array or any collection.
2020

@@ -42,17 +42,15 @@ I have given a simple Java program to traverse over an array using the for-each
4242
*/
4343
public class ArrayForEachExample {
4444

45-
public static void main(String[] args) {
46-
47-
//Create an array of marks
48-
int[] numbers = { 88, 95, 65, 76, 78, 45, 95, 96, 56};
49-
50-
// traversing the array using for-each loop
51-
for(int number: numbers) {
52-
53-
System.out.println("The number is "+ number);
54-
}
55-
}
45+
public static void main(String[] args) {
46+
//Create an array of marks
47+
int[] numbers = { 88, 95, 65, 76, 78, 45, 95, 96, 56};
48+
49+
// traversing the array using for-each loop
50+
for(int number: numbers) {
51+
System.out.println("The number is "+ number);
52+
}
53+
}
5654
}
5755
```
5856
Output:

0 commit comments

Comments
 (0)