B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Java do-while Loop | TutorialsApp

Java do-while Loop

Explanation (English): A do-while loop is similar to a while loop, but the condition is checked after the loop body. That means the code will run at least once even if the condition is false.

व्याख्या (हिंदी): do-while loop, while loop जैसा होता है लेकिन इसमें condition बाद में चेक होती है। इसलिए loop का body कम से कम एक बार जरूर चलेगा, चाहे condition false हो।

Syntax:
do {
    // code to execute
} while (condition);
Example:
public class DoWhileExample {
    public static void main(String[] args) {
        int i = 1;
        do {
            System.out.println("Count: " + i);
            i++;
        } while (i <= 3);
    }
}

Output:
Count: 1
Count: 2
Count: 3

आउटपुट:
Count: 1
Count: 2
Count: 3

🔁 Comparison: while vs do-while Loop
Feature while loop do-while loop
Condition Check Before loop body (entry-controlled) After loop body (exit-controlled)
Minimum Executions 0 times (if condition is false initially) At least 1 time (runs once before checking condition)
Syntax while(condition) { ... } do { ... } while(condition);
Use Case When loop should run only if condition is true When loop must run at least once
विशेषता while लूप do-while लूप
कंडीशन चेक लूप से पहले (entry-controlled) लूप के बाद (exit-controlled)
कम से कम बार चलना 0 बार (अगर condition शुरू से false हो) कम से कम 1 बार जरूर चलेगा
सिंटैक्स while(condition) { ... } do { ... } while(condition);
कब उपयोग करें जब लूप condition सही होने पर ही चले जब लूप को कम से कम 1 बार चलाना हो