← Back to C Course | Chapter 4: Control Flow | Lesson 8 of 11

C का do-while Loop

एक do-while loop पहले steps करता है और बाद में check करता है, salt add करने का decide करने से पहले खाना taste करने जैसा। यह हमेशा कम से कम एक बार चलता है।
Syntax
c
do {
  // body runs at least once
} while (condition);

do-while Loop क्या है?

एक do-while loop एक post-test loop है: यह पहले अपनी body चलाता है और सिर्फ बाद में condition check करता है, कम से कम एक execution guarantee करते हुए भले ही condition शुरू से false होती।

उदाहरण: What is a do-while Loop?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 10
	int x = 10;
	// do-while: run the body once, then repeat while the condition is true
	do {
		// Print formatted text to the screen
		printf("Runs at least once");
	} while (x < 5);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Simple Countdown

आप हर pass पर body के अंदर counter को decrement करके एक do-while loop से एक countdown sequence print कर सकते हैं, एक while loop जैसा ही, लेकिन इस guarantee के साथ कि पहली value कम से कम एक बार हमेशा print होती है।

उदाहरण: Simple Countdown

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare i and set it to 5
	int i = 5;
	// do-while: run the body once, then repeat while the condition is true
	do {
		// Print formatted text to the screen
		printf("%d ", i);
		i--;
	} while (i >= 1);
	// Return 0 to signal that the program finished successfully
	return 0;
}

User Input Validation

do-while input validation के लिए अच्छी तरह suited है, क्योंकि आपको check करने के लिए कुछ होने से पहले आमतौर पर कम से कम एक बार user को prompt करना और उनका input पढ़ना होता है -- एक plain while loop यह पहला prompt होने की guarantee नहीं दे सकता।

उदाहरण: User Input Validation

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int value;
	// do-while: run the body once, then repeat while the condition is true
	do {
		sscanf("7", "%d", &value);
	} while (value < 0);
	// Print formatted text to the screen
	printf("%d", value);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Sum Accumulation

एक while loop की तरह ही, एक do-while loop हर बार execute होने पर अपनी body के अंदर एक sum variable update करके iterations में एक running total accumulate कर सकता है।

उदाहरण: Sum Accumulation

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int i = 1, sum = 0;
	// do-while: run the body once, then repeat while the condition is true
	do {
		sum += i;
		i++;
	} while (i <= 5);
	// Print formatted text to the screen
	printf("%d", sum);
	// Return 0 to signal that the program finished successfully
	return 0;
}

while बनाम do-while

एक while loop से key फर्क timing है: while पहली run से पहले अपनी condition check करता है और zero बार execute हो सकता है, जबकि do-while पहली run के बाद check करता है और हमेशा कम से कम एक बार execute होता है।

उदाहरण: while vs. do-while

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 10
	int x = 10;
	// Repeat while x < 5 is true
	while (x < 5) {
		// Print formatted text to the screen
		printf("while: never runs\n");
	}
	// do-while: run the body once, then repeat while the condition is true
	do {
		// Print formatted text to the screen
		printf("do-while: runs once");
	} while (x < 5);
	// Return 0 to signal that the program finished successfully
	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 भूल जाना।
  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.