-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathForEachLoopDemo.java
More file actions
40 lines (29 loc) · 1.03 KB
/
ForEachLoopDemo.java
File metadata and controls
40 lines (29 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package Iterable.Examples;
import java.util.ArrayList;
import java.util.List;
public class ForEachLoopDemo {
public static void main(String[] args) {
List<String> students = new ArrayList<>();
students.add("Alex");
students.add("Jordan");
students.add("Taylor");
students.add("Morgan");
System.out.println("Printing students using a for-each loop:");
// Loop through each student and print their name
for (String student : students) {
System.out.println(student);
}
System.out.println("\nPrinting students in uppercase:");
// Loop through each student and print name in uppercase
for (String student : students) {
System.out.println(student.toUpperCase());
}
System.out.println("\nCount the number of students:");
int count = 0;
// Loop through students and increment count
for (String student : students) {
count++;
}
System.out.println("Total students: " + count);
}
}