Java do while Loop In Hindi

📔 : Java 🔗

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

Java do while loop syntax

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

Java do while loop chart flow

Java do while Loop Chart Flow

Java do while loop example

File : DoWhileLoop.java

CopyFullscreenClose FullscreenRun
public class DoWhileLoop {
  public static void main(String[] args) {
    int num = 1;
    do {
      System.out.println(num);
      // increase value by one using post increment.
      num++;
    } while (num <= 10);
  }
}
Output
javac DoWhileLoop.java
java DoWhileLoop
1
2
3
4
5
6
7
8
9
10

पिछले topic में 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 होना ही है।
See Example :

File : DoWhileLoop.java

CopyFullscreenClose FullscreenRun
public class DoWhileLoop {
  public static void main(String[] args) {
    // first let's try to run code of block in while loop.
    int num=1;
    while (num < 1) // it is false condition.
    {
      System.out.println("It is not goinf to print.");
    }
    
    // now try to print with do while loop.
    do {
      System.out.println("Yes ! do while loop is running.");
    } while (false);
  }
}
Output
javac DoWhileLoop.java
java DoWhileLoop
Yes ! do while loop is running.

Java Infinitive do-while Loop

do while Loop किसी भी तरह के Loop को infinite run करने के लिए pass होने वाली condition true होनी चाहिए। do while Loop में आपको कोई भी ऐसा expression देना है जो कि हमेशा true value return करता हो।

do
{  
    //code of block to be executed  
} while(true);

हालाँकि कभी भी किसी भी loop को infinite run नहीं करना चाहिए अलग run करते भी तो उस program को आप ctrl + c से terminate कर सकते हो।
See Below Example -

File : InfiniteLoop.java

CopyFullscreenClose FullscreenRun
public class InfiniteLoop {
  public static void main(String[] args) {
    do {
      System.out.println("it will run infinite...");
    } while (true); // pass true so that it can run infinite times.
  }
}
Output
javac InfiniteLoop.java
java InfiniteLoop
it will run infinite...
it will run infinite...
it will run infinite...
it will run infinite...
it will run infinite...
it will run infinite...
it will run infinite...
it will run infinite...
it will run infinite...
it will run infinite...
it will run infinite...
ctrl+c

while Loop की तरह ही आप जरूरत पड़ने पर nested do while Loop भी use कर सकते हैं। I Hope, अब आप Java में do while Loop के बारे में अच्छे से समझ गए होंगे और 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