-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloops.java
More file actions
33 lines (28 loc) · 769 Bytes
/
loops.java
File metadata and controls
33 lines (28 loc) · 769 Bytes
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
public class loops{
public static void main(String[] args) {
// for loop
for (int i = 0; i < 5; i++) {
System.out.println("for loop: " + i);
}
// while loop
int i = 0;
while (i < 5) {
System.out.println("while loop: " + i);
i++;
}
// do-while loop (simulated using a while loop with a condition)
i = 0;
while (true) {
System.out.println("do-while loop: " + i);
i++;
if (i == 5) {
break;
}
}
// for-each loop
int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
System.out.println("for-each loop: " + number);
}
}
}