Breaking

Search This Blog

Sunday, 11 November 2018

do-while Loop in java

Do-while Loop Java


do-while loop is similar to a while loop, except that a do-while loop is guaranteed to execute at least one time.

एक do-whileलूप थोड़ी देर के लूप के समान होता है, सिवाय इसके कि एक ... जबकि लूप को कम से कम एक बार निष्पादित करने की गारंटी दी जाती है

Syntax

Following is the syntax of a do-while loop −
do {
   // Statements
}while(condition);  


Example


public class Test {

   public static void main(String args[]) {
      int x = 10;

      do {
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }while( x < 20 );
   }
}
This will produce the following result −

Output

value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19

Infinitive do-while Loop in Java

If you pass true in the do-while loop, it will be infinitive do-while loop

यदि आप डू-लूप लूप में सच हो जाते हैं, तो यह भयावह डू-लूप होगा

Syntax:
  1. do{  
  2. //code to be executed  
  3. }while(true);  
Example:
  1. public class DoWhileExample2 {  
  2. public static void main(String[] args) {  
  3.     do{  
  4.         System.out.println("infinitive do while loop");  
  5.     }while(true);  
  6. }  
  7. }  
Output:
infinitive do while loop
infinitive do while loop
infinitive do while loop
ctrl+c
Now, you need to press ctrl+c to exit from the program




Check out these related programs:

  1. Java Program to Check if a Given Integer is Odd or Even
  2. how to run java program in CMD
  3. For Loops in java [hindi and english both]
  4. Hello : How to Create Your First Java Program
  5. How to set Environment Variables in Java: Path and Classpath
  6. while loop in java [Hindi and English Both]

No comments:

Post a Comment

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();