20
Q:

what is a do while loop in java

do {
  //something you want to execute at least once
} while (someBooleanCondition);
3
int i = 0;
while (i < 5) {
  System.out.println(i);
  i++;
}
5
// Iterate Array using while loop
public class ArrayWhileLoop
{
   public static void main(String[] args) 
   {
      int[] arrNumbers = {2, 4, 6, 8, 10, 12};
      int a = 0;
      System.out.println("Printing even numbers: ");
      while(a < 6)
      {
         System.out.println(arrNumbers[a]);
         a++;
      }
   }
}
1
// Java infinite while loop
import java.util.*;
public class WhileLoopExample
{
   public static void main(String[] args)
   {
      boolean value = true;
      while(value)
      {
         System.out.println("Infinite loop");
      }
   }
}
1
public class WhileLoopDemo
{
   public static void main(String args[])
   {
      int a = 1;
      while(a < 10)
      {
         System.out.println(a);
         a++;
         System.out.print("\n");
      }
   }
}
1
// java while loop break
public class JavaWhileLoopBreak 
{
   public static void main(String[] args) 
   {
      int a = 1;  
      while(a <= 20)
      {  
         if(a == 6)
         {  
            a++;  
            break; // break the loop  
         }  
         System.out.println(a);  
         a++;  
      }
   }
}
1
do while loop java executes the statements and then evaluates the 
expression/condition of loop’s body until given condition is true.
Here’s do while loop syntax in java.

// do while loop java
do{
   statement(s);
}while(expression/condition);
2
A while loop iterates a block of statements until condition is true. In a 
while loop condition is executed first.
Syntax:

while(condition)
{
   // code goes here
}
1
int i = 0;
while (i < 5) {
  System.out.println(i);
  i++;
}
2
//runs as long as the condition is true
while(condition){
//do what you want in here
doStuff()
}
4

New to Communities?

Join the community