If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
पिछले Topic में आपने While Loop के बारे में पढ़ा इस topic में आप do while Loop के बारे में पढ़ेंगे।
जैसा कि नाम से समझ आ रहा है कि पहले कोई code of block run हो रहा है। यह While Loop की तरह ही work करता है , बस इसमें पहले code of block run होता है उसके बाद condition check होती है। condition false होने loop से बाहर आजएंगे और true होने पर फिर से same code of block run होगा।
do { //code of block } while(condition / expression);
#include <stdio.h>
int main() {
int num = 1;
do
{
printf("%d\n", num);
// increase value by one using post increment.
num++;
}
while (num <= 10);
return 0;
}
1 2 3 4 5 6 7 8 9 10
while loop के same example को do while loop के through किया है , आप output में देख सकते हैं कि output same ही आया है , बस program का structure change हो गया है। well , do while loop हम वहां पर use करते हैं , जब हमें किसी loop के अंदर का code of block कम से कम एक बार तो code of block run करना ही हो।
while loop और do while loop में यही main difference भी है , while loop में सबसे पहले condition ही check होती है उसके बाद ही code of block run होता है , अगर condition false है तो loop में entry ही नहीं होगी , उसके उलट do while loop में सबसे पहले code of block run होगा और सबसे end में condition check होती है , इससे कोई फर्क नहीं पड़ता कि condition सही है या गलत , Loop को एक बार Run होना ही है।
#include <stdio.h>
// include stdbool header file to work with boolean value.
#include <stdbool.h>
int main() {
bool status = false;
// first try to print a line with while loop,
while(status) {
printf("while loop");
}
// now do the same thing with do while loop.
do {
printf("do while loop");
}
while(status);
return 0;
}
do while loop
I Hope, Example देखकर अब आप अच्छे से समझ गए होंगे कि while loop और do while loop में difference क्या है, और इनका purpose क्या है।
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook