Java का do-while Loop
In this page:
do {
// runs at least once
} while (condition);
Basic do-while Loop
एक do-while loop पहले अपना body चलाता है और सिर्फ बाद में, आखिर में, condition जांचता है -- एक regular while loop से विपरीत order, जो body में enter होने से पहले condition test करता है।
उदाहरण: Basic do-while Loop
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare i and set it to 0
int i = 0;
// do-while: run the body once, then repeat while the condition is true
do {
// Print a line to the console
System.out.println(i);
i++;
} while (i < 3);
}
}
Login to try C/C++/Java/PHP code in the editor
Guaranteeing One Run
यह ordering गारंटी देता है कि loop body कम से कम एक बार execute होता है भले ही condition बिल्कुल शुरुआत से false होती, जो एक standard while loop कभी गारंटी नहीं दे सकता।
उदाहरण: Guaranteeing One Run
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare i and set it to 10
int i = 10;
// do-while: run the body once, then repeat while the condition is true
do {
// Print a line to the console
System.out.println("Runs once: " + i);
} while (i < 5);
}
}
Login to try C/C++/Java/PHP code in the editor
Incrementing Variables inside Loop
आपको अपने loop condition में जांचे गए variable को do-while block के अंदर कहीं modify करना चाहिए, नहीं तो loop हमेशा के लिए repeat करता रहेगा क्योंकि condition को कभी false होने का मौका नहीं मिलता।
उदाहरण: Incrementing Variables inside Loop
public class Main {
public static void main(String[] args) {
int i = 0;
do {
System.out.println(i);
i++; // without this, the loop never ends
} while (i < 3);
}
}
Login to try C/C++/Java/PHP code in the editor
Simulated Interactive Runs
किसी भी loop की तरह, आपको condition निर्भर करने वाले variable को do-while body के अंदर कहीं update करना चाहिए -- अगर वह variable कभी नहीं बदलता, loop शुरू होने पर हमेशा के लिए चलेगा।
उदाहरण: Simulated Interactive Runs
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare attempts and set it to 0
int attempts = 0;
// do-while: run the body once, then repeat while the condition is true
do {
attempts++;
// Print a line to the console
System.out.println("Attempt " + attempts);
} while (attempts < 3);
}
}
Login to try C/C++/Java/PHP code in the editor
Nested do-while Loops
एक do-while को दूसरे do-while के अंदर nest करना आपको एक grid जैसी structures बनाने देता है जहां हर outer iteration हमेशा inner loop का कम से कम एक पूरा pass process करता है, guaranteed।
उदाहरण: Nested do-while Loops
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare row and set it to 1
int row = 1;
// do-while: run the body once, then repeat while the condition is true
do {
// Declare col and set it to 1
int col = 1;
// do-while: run the body once, then repeat while the condition is true
do {
// Print to the console without a newline
System.out.print(col + " ");
col++;
} while (col <= 3);
// Print a line to the console
System.out.println();
row++;
} while (row <= 2);
}
}
Login to try C/C++/Java/PHP code in the editor
- एक do-while में
while (condition)के बाद semicolon भूल जाना, जो एक compile error है। - Condition false होने पर body skip होने की उम्मीद करना, जब do-while हमेशा एक बार चलता है।
- Body के अंदर एक variable declare करना और इसे
whilecondition में उपयोग करना, जहां यह scope से बाहर है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: