🟡 Control Flow · Lesson 12
Loops (for, while, do-while)
Loops (for, while, do-while)
Why Loops?
A loop repeats code. Java has
for, while and do-while.for Loop
for (int i = 1; i <= 5; i++) {
System.out.print(i + " ");
}1 2 3 4 5
while and do-while
int i = 1;
while (i <= 3) {
System.out.println(i);
i++;
}
int j = 1;
do {
System.out.println(j);
j++;
} while (j <= 3);
Three Loops
| Loop | Use When |
|---|---|
| for | count is known |
| while | repeat until condition false |
| do-while | run at least once |
Summary
- for = known count; while = condition-based; do-while = runs at least once.
Loops क्यों?
Loop code दोहराता है। Java में
for, while और do-while हैं।for Loop
for (int i = 1; i <= 5; i++) {
System.out.print(i + " ");
}1 2 3 4 5
while और do-while
int i = 1;
while (i <= 3) {
System.out.println(i);
i++;
}
int j = 1;
do {
System.out.println(j);
j++;
} while (j <= 3);
तीन Loops
| Loop | कब Use करें |
|---|---|
| for | count पता हो |
| while | condition false होने तक |
| do-while | कम से कम एक बार चले |
सारांश
- for = ज्ञात count; while = condition-based; do-while = कम से कम एक बार।