1//runs as long as the condition is true
2while(condition){
3//do what you want in here
4doStuff()
5}
1// Java infinite while loop
2import java.util.*;
3public class WhileLoopExample
4{
5 public static void main(String[] args)
6 {
7 boolean value = true;
8 while(value)
9 {
10 System.out.println("Infinite loop");
11 }
12 }
13}
1public class WhileLoopDemo
2{
3 public static void main(String args[])
4 {
5 int a = 1;
6 while(a < 10)
7 {
8 System.out.println(a);
9 a++;
10 System.out.print("\n");
11 }
12 }
13}
1A while loop iterates a block of statements until condition is true. In a
2while loop condition is executed first.
3Syntax:
4
5while(condition)
6{
7 // code goes here
8}
1// Iterate Array using while loop
2public class ArrayWhileLoop
3{
4 public static void main(String[] args)
5 {
6 int[] arrNumbers = {2, 4, 6, 8, 10, 12};
7 int a = 0;
8 System.out.println("Printing even numbers: ");
9 while(a < 6)
10 {
11 System.out.println(arrNumbers[a]);
12 a++;
13 }
14 }
15}