← Back to C++ Course | Chapter 4: Control Flow | Lesson 10 of 13

C++ का do-while Loop

एक do-while loop पहले steps करता है और बाद में check करता है, salt add करने का decide करने से पहले खाना taste करने जैसा। यह हमेशा कम से कम एक बार चलता है।
Syntax
cpp
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

cpp
// 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;
}

कम से कम एक Run की Guarantee

एक while loop के उलट, जो अपनी body कभी नहीं चला सकता अगर condition शुरुआत में false हो, एक do-while loop condition evaluate होने से भी पहले हमेशा कम से कम एक पूरा pass execute करता है।

यह इसे सही structural choice बनाता है जब भी logic को genuinely 'इसे कम से कम एक बार करो, फिर ज़रूरत पड़े तो दोहराओ' चाहिए।

उदाहरण: Guaranteeing At Least One Run

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक do-while के अंत में while (condition) के बाद semicolon भूल जाना, जो एक compile error cause करता है।
  2. यह भूल जाना कि body condition false होने पर भी कम से कम एक बार चलती है।
  3. condition में variable कभी न बदलना, एक infinite loop बनाते हुए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.