C do while Loop In Hindi

📔 : C 🔗

पिछले 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 होगा।

C do while loop syntax

do
{
  //code of block
}
while(condition / expression);

C do while loop chart flow

C do while Loop Chart Flow

C do while loop example

CopyFullscreenClose FullscreenRun
#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;
}
Output
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 करना ही हो।

Difference between while loop And do while loop

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 होना ही है।

CopyFullscreenClose FullscreenRun
#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;
}
Output
do while loop

I Hope, Example देखकर अब आप अच्छे से समझ गए होंगे कि while loop और do while loop में difference क्या है, और इनका purpose क्या है।

Related Topics :

Rahul Kumar

Rahul Kumar

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

b2eprogrammers