-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ5.java
More file actions
60 lines (53 loc) · 1.49 KB
/
Copy pathJ5.java
File metadata and controls
60 lines (53 loc) · 1.49 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
> Date Created: 18/04/2026
> Author: Ishaan Rastogi
> Purpose: To showcase loops in java
> Operating System: This is only for Windows OS, it may or may not work on other OS
> Program Status: 100% Working
*/
public class J5 {
public static void main(String[] args) {
// for
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
System.out.println();
for (int a = 1; a <= 10; a += 2) {
System.out.println(a);
}
System.out.println();
// Nested for
for (int x = 1; x <= 3; x++) {
for (int y = 1; y <= 3; y++) {
System.out.print("* ");
}
System.out.println(); // println - next line
}
System.out.println();
// for-each -> for (Datatype variable : Collection) {}
// Use- When you want to iterate over any collection.
String[] cars = { "Volvo", "Maruti", "Hyundai", "Honda" };
for (String car : cars) {
System.out.println(car);
}
System.out.println();
int[] numbers = { 1, 2, 3, 4, 5 };
for (int i : numbers) {
System.out.println(i);
}
System.out.println();
// while
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
System.out.println();
// do-while
int j = 1;
do {
System.out.println(j);
j++;
} while (j <= 5);
}
}