C++ का do-while Loop
In this page:
do {
// body runs at least once
} while (condition);
Basic do-while Loop
एक do-while loop एक post-test loop है: यह पहले body चलाता है और सिर्फ बाद में, loop के bottom पर, condition check करता है, जो एक regular while loop के condition-first check से opposite order है।
यही structural difference है जो guarantee करता है कि loop body हमेशा कम से कम एक बार execute हो।
उदाहरण: Basic do-while Loop
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare count and set it to 0
int count = 0;
// do-while: run the body once, then repeat while the condition is true
do {
// Print to the console with cout
std::cout << count << std::endl;
count++;
} while (count < 5);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
कम से कम एक Run की Guarantee
एक while loop के उलट, जो अपनी body कभी नहीं चला सकता अगर condition शुरुआत में false हो, एक do-while loop condition evaluate होने से भी पहले हमेशा कम से कम एक पूरा pass execute करता है।
यह इसे सही structural choice बनाता है जब भी logic को genuinely 'इसे कम से कम एक बार करो, फिर ज़रूरत पड़े तो दोहराओ' चाहिए।
उदाहरण: Guaranteeing At Least One Run
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare count and set it to 10
int count = 10;
// do-while: run the body once, then repeat while the condition is true
do {
// Print to the console with cout
std::cout << "Runs once even though condition is false" << std::endl;
count++;
} while (count < 5);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
User Menu Choices Simulate करना
Interactive menus do-while के लिए एक textbook use case हैं, क्योंकि आप naturally menu दिखाना और check करने के लिए कुछ होने से पहले user की पहली choice पाना चाहते हैं — loop फिर user के exit option चुनने तक menu दिखाना दोहराता है।
एक regular while loop को अपनी condition evaluate होने से पहले menu दिखाने के लिए एक awkward workaround चाहिए होगा।
उदाहरण: Simulating User Menu Choices
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int choice;
// Declare attempts and set it to 0
int attempts = 0;
int hardcodedInputs[] = {5, 2};
// do-while: run the body once, then repeat while the condition is true
do {
choice = hardcodedInputs[attempts];
// Print to the console with cout
std::cout << "Menu choice: " << choice << std::endl;
attempts++;
} while (choice != 2);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Data Validation Simulation
Input validation same pattern follow करता है: आपको यह check करने से पहले कि यह valid है या नहीं actually input के लिए prompt करना और इसे receive करना होगा, जो exactly वह 'पहले act करो, बाद में check करो' order है जो एक do-while loop देता है।
यह एक while loop के upfront condition check को satisfy करने के लिए सिर्फ एक initial value fake करने की ज़रूरत से बचाता है।
उदाहरण: Data Validation Simulation
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int age;
int hardcodedInputs[] = {-5, 25};
// Declare attempts and set it to 0
int attempts = 0;
// do-while: run the body once, then repeat while the condition is true
do {
age = hardcodedInputs[attempts];
// Print to the console with cout
std::cout << "Entered age: " << age << std::endl;
attempts++;
} while (age < 0);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nested do-while Loops
do-while loops को एक-दूसरे के अंदर nest करना किसी भी दूसरे loop type को nest करने जैसा ही काम करता है, और grid-based patterns बनाने या multi-dimensional data process करने के लिए useful है जहाँ हर dimension को किसी condition की परवाह किए बिना कम से कम एक बार चलना ज़रूरी है, जैसे एक fixed-size table generate करना।
उदाहरण: Nested do-while Loops
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare i and set it to 1
int i = 1;
// do-while: run the body once, then repeat while the condition is true
do {
// Declare j and set it to 1
int j = 1;
// do-while: run the body once, then repeat while the condition is true
do {
// Print to the console with cout
std::cout << i << "," << j << " ";
j++;
} while (j <= 2);
// Print to the console with cout
std::cout << std::endl;
i++;
} while (i <= 2);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक do-while के अंत में
while (condition)के बाद semicolon भूल जाना, जो एक compile error cause करता है। - यह भूल जाना कि body condition false होने पर भी कम से कम एक बार चलती है।
- condition में variable कभी न बदलना, एक infinite loop बनाते हुए।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: