-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoopsExample.java
More file actions
47 lines (43 loc) · 970 Bytes
/
Copy pathLoopsExample.java
File metadata and controls
47 lines (43 loc) · 970 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Day 3 - Control Flow: Loops (for, while, do-while)
*
* Concept:
* Loops are used to execute a block of code repeatedly
* based on a condition.
*
* Types covered:
* - for loop
* - while loop
* - do-while loop
*
* Real-life analogy:
* Repeating actions like:
* - Walking steps
* - Daily routines
*/
public class LoopsExample {
/**
* Main method demonstrating different loops
*/
public static void main(String[] args) {
// for loop
System.out.println("For Loop:");
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
// while loop
System.out.println("\nWhile Loop:");
int j = 1;
while (j <= 5) {
System.out.println(j);
j++;
}
// do-while loop
System.out.println("\nDo-While Loop:");
int k = 1;
do {
System.out.println(k);
k++;
} while (k <= 5);
}
}