While Loop

Example of While and Do while Loop
package basic;

public class JavawhileLoop {
	
	public static void main(String arg[])
	
	{
		int a=0;
		
		 while(a < 10)
		{
			 System.out.println(a);
			 a++;
			
		}
		 
		 int b=2;
		 do
		 {
			 System.out.println("value of b is "+ b);
			 b++;
		 }
		 while(b < 1);
		
	}

}

output:
0
1
2
3
4
5
6
7
8
9
value of b is 2

Difference between while and do while Loop

1. while loop

  • It will execute until condition is true.

2. do while loop

  • It will first execute after that it will check condition.
    so it will execute at least once.

Leave a Reply