🟡 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

LoopUse When
forcount is known
whilerepeat until condition false
do-whilerun 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 करें
forcount पता हो
whilecondition false होने तक
do-whileकम से कम एक बार चले

सारांश

  • for = ज्ञात count; while = condition-based; do-while = कम से कम एक बार।
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n