While Loop

Example of While and Do while Loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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

Your email address will not be published. Required fields are marked *