C का do-while Loop
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Simple Countdown
आप हर pass पर body के अंदर counter को decrement करके एक do-while loop से एक countdown sequence print कर सकते हैं, एक while loop जैसा ही, लेकिन इस guarantee के साथ कि पहली value कम से कम एक बार हमेशा print होती है।
उदाहरण: Simple Countdown
// 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;
}
Login to try C/C++/Java/PHP code in the editor
User Input Validation
do-while input validation के लिए अच्छी तरह suited है, क्योंकि आपको check करने के लिए कुछ होने से पहले आमतौर पर कम से कम एक बार user को prompt करना और उनका input पढ़ना होता है -- एक plain while loop यह पहला prompt होने की guarantee नहीं दे सकता।
उदाहरण: User Input Validation
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Sum Accumulation
एक while loop की तरह ही, एक do-while loop हर बार execute होने पर अपनी body के अंदर एक sum variable update करके iterations में एक running total accumulate कर सकता है।
उदाहरण: Sum Accumulation
// 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;
}
Login to try C/C++/Java/PHP code in the editor
while बनाम do-while
एक while loop से key फर्क timing है: while पहली run से पहले अपनी condition check करता है और zero बार execute हो सकता है, जबकि do-while पहली run के बाद check करता है और हमेशा कम से कम एक बार execute होता है।
उदाहरण: while vs. do-while
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक do-while के अंत में
while (condition)के बाद semicolon भूल जाना। - यह भूल जाना कि body हमेशा एक बार चलती है भले ही condition false हो।
- condition में variable कभी न बदलना, एक infinite loop बनाते हुए।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: