while loop in Java Programming Language

Rumman Ansari   Software Engineer   2019-03-30   8430 Share
☰ Table of Contents

Table of Content:


while Loop in Java

The while loop is Java’s most fundamental loop statement. while loop executes statements repeatedly while the condition is true.

while loop in java

Syntax for the while loop

while (loop-continuation-condition) {
// Loop body
Statement(s);
}

loop-continuation-condition:

The condition can be any Boolean expression.

Loop body:

The body of the loop will be executed as long as the conditional expression is true.When condition becomes false, control passes to the next line of code immediately following the loop. A one-time execution of a loop body is referred to as an iteration (or repetition) of the loop

Braces { }:

The curly braces are unnecessary if only a single statement is being repeated.

Example:

The loop for displaying Welcome to Java! a hundred times.

// Java program to demonstrate while loop in Java
class WhileLoopExample
{
    public static void main(String args[])
    {
       int count = 0;
	   while(count < 100){
	   System.out.println("Welcome to atnyla!");
	   count++;
       }
    }
}

Output:

Welcome to atnyla!
Welcome to atnyla!
Welcome to atnyla!
............
............
............
Welcome to atnyla!
Welcome to atnyla!
Welcome to atnyla!
Press any key to continue . . .

Explanation:

The variable count is initially 0 . The loop checks whether count< 100 is true . If so, it executes the loop body to display the message Welcome to atnyla! and increments count by 1 . It repeatedly executes the loop body until count< 100 becomes false. When count< 100 is false (i.e., when count reaches 100 ), the loop terminates and the next statement after the loop statement is executed.

The loop-continuation-condition must always appear inside the parentheses. The braces enclosing the loop body can be omitted only if the loop body contains one or no statement.

While Loop with no body

while may not have body part. Semicolon after while condition is perfectly legal in while loop.

boolean a = true ; 
while(a); 

Example:

class WhileLoopNoBody {
    public static void main(String[] args){
	int i, j;

    i = 10;
    j = 20;
    System.out.println(i);
    System.out.println(j);

    while(i < j);  // no body in this loop

    }
}

Output:

10
20

Another Example of while loop:

public class WhileLoopExample {
public static void main(String[] args) {
    int n=1;
    while(n<=10){
        System.out.println(n);
    n++;
    }
  }
}

Output:

1
2
3
4
5
6
7
8
9
10
Press any key to continue . . .

Single Statement Inside while Loop Body

Single Statement is a part of While Loop as there is no opening and closing curly braces. If we want to write multiple statements as a part of while loop body then we have to put all the statements in a block. i.e the { }

while(true)   
//code to be executed  or statement(single); 
  

Example of while loop:

class WhileLoopSingleStatement {
    public static void main(String[] args){
        int count = 1;
        while (count <= 11)
         System.out.println("Number Count : " + count++);
    }
}

Output:

Number Count : 1
Number Count : 2
Number Count : 3
Number Count : 4
Number Count : 5
Number Count : 6
Number Count : 7
Number Count : 8
Number Count : 9
Number Count : 10
Number Count : 11
Press any key to continue . . .

Note: Make sure that the loop-continuation-condition eventually becomes false so that the loop will terminate. A common programming error involves infinite loops (i. e., the loop runs forever). If your program takes an unusually long time to run and does not stop, it may have an infinite loop. If you are running the program from the command window, press CTRL+C to stop it.

Java Infinitive While Loop

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

while(true){  
//code to be executed  or statement(s); 
}  

Example 1:

public class WhileInfiniteLoop {
public static void main(String[] args) {
    while(true){
        System.out.println("infinitive while loop");
    }
  }
}

Output:

infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
.........
.........
.........
.........
infinite time it will print like this
 

Example 2:

class InfiniteWhileLoop {
    public static void main(String[] args){
	int n = 0;
		while (n == n) {
		System.out.println("Thanks for visiting our website");
		n++;
	  }
    }
}

Output:

Thanks for visiting our website
Thanks for visiting our website
Thanks for visiting our website
Thanks for visiting our website
Thanks for visiting our website
Thanks for visiting our website
Thanks for visiting our website
Thanks for visiting our website
Thanks for visiting our website
Thanks for visiting our website
Thanks for visiting our website
Thanks for visiting our website 
Thanks for visiting our website
Thanks for visiting our website
Thanks for visiting our website
...............................
...............................
...............................
...............................
...............................
infinite time it will print like this
 

Boolean Condition inside while loop

We can put single variable as while condition but it must be of type boolean.

boolean a = true ;
boolean a = false ;
while(a){  
//code to be executed  or statement(s); 
}  

Example:

class WhileLoopBoolean {
    public static void main(String[] args){
        boolean a = true;
        int count = 0 ;
        while (a)
            {
            System.out.println("Number Count : " + count);
            count++;
            if(count==5)
               a = false;
            }
    }
}

Output:

Number Count : 0
Number Count : 1
Number Count : 2
Number Count : 3
Number Count : 4
Press any key to continue . . . 

Non Boolean varible inside while loop condition

We can't put single variable as while condition but it must be of type boolean. If we will give non boolean value inside while loop, it give us a compile time error.(incompatible type) also int cannot be converted to boolean using casting

int a = true ; 
while(a){    // this will give you error
//code to be executed  or statement(s); 
}  

Example:

class WhileLoopBoolean {
    public static void main(String[] args){
        int a = 10;
        int count = 0 ;

		while(a)
		  {
		  System.out.println("Number Count : " + count);
		  count++;
		  if(count==5)
		      a = 1;
        }
    }
}

Output:

WhileLoopBoolean.java:6: error: incompatible types: int cannot be converted to boolean
		while(a)
		      ^
1 error

Common Error in Java Program

off-by-one error

Programmers often make the mistake of executing a loop one more or less time. This is commonly known as the off-by-one error.Example:

class OffByOneError {
    public static void main(String[] args){
	int number = 0;
	   while (number <= 100 ) {
	   System.out.println(number+" Hello Dhinchak Pooja! \t Selfie mayne la liya!!");
	   number++;
       }
    }
}

Output:

0 Hello Dhinchak Pooja!          Selfie mayne la liya!!
1 Hello Dhinchak Pooja!          Selfie mayne la liya!!
2 Hello Dhinchak Pooja!          Selfie mayne la liya!!
3 Hello Dhinchak Pooja!          Selfie mayne la liya!!
4 Hello Dhinchak Pooja!          Selfie mayne la liya!!
5 Hello Dhinchak Pooja!          Selfie mayne la liya!!
6 Hello Dhinchak Pooja!          Selfie mayne la liya!!
7 Hello Dhinchak Pooja!          Selfie mayne la liya!!
8 Hello Dhinchak Pooja!          Selfie mayne la liya!!
9 Hello Dhinchak Pooja!          Selfie mayne la liya!!
10 Hello Dhinchak Pooja!         Selfie mayne la liya!!
11 Hello Dhinchak Pooja!         Selfie mayne la liya!!
12 Hello Dhinchak Pooja!         Selfie mayne la liya!!
13 Hello Dhinchak Pooja!         Selfie mayne la liya!!
14 Hello Dhinchak Pooja!         Selfie mayne la liya!!
15 Hello Dhinchak Pooja!         Selfie mayne la liya!!
16 Hello Dhinchak Pooja!         Selfie mayne la liya!!
17 Hello Dhinchak Pooja!         Selfie mayne la liya!!
18 Hello Dhinchak Pooja!         Selfie mayne la liya!!
19 Hello Dhinchak Pooja!         Selfie mayne la liya!!
20 Hello Dhinchak Pooja!         Selfie mayne la liya!!
21 Hello Dhinchak Pooja!         Selfie mayne la liya!!
22 Hello Dhinchak Pooja!         Selfie mayne la liya!!
23 Hello Dhinchak Pooja!         Selfie mayne la liya!!
24 Hello Dhinchak Pooja!         Selfie mayne la liya!!
25 Hello Dhinchak Pooja!         Selfie mayne la liya!!
26 Hello Dhinchak Pooja!         Selfie mayne la liya!!
27 Hello Dhinchak Pooja!         Selfie mayne la liya!!
28 Hello Dhinchak Pooja!         Selfie mayne la liya!!
29 Hello Dhinchak Pooja!         Selfie mayne la liya!!
30 Hello Dhinchak Pooja!         Selfie mayne la liya!!
31 Hello Dhinchak Pooja!         Selfie mayne la liya!!
32 Hello Dhinchak Pooja!         Selfie mayne la liya!!
33 Hello Dhinchak Pooja!         Selfie mayne la liya!!
34 Hello Dhinchak Pooja!         Selfie mayne la liya!!
35 Hello Dhinchak Pooja!         Selfie mayne la liya!!
36 Hello Dhinchak Pooja!         Selfie mayne la liya!!
37 Hello Dhinchak Pooja!         Selfie mayne la liya!!
38 Hello Dhinchak Pooja!         Selfie mayne la liya!!
39 Hello Dhinchak Pooja!         Selfie mayne la liya!!
40 Hello Dhinchak Pooja!         Selfie mayne la liya!!
41 Hello Dhinchak Pooja!         Selfie mayne la liya!!
42 Hello Dhinchak Pooja!         Selfie mayne la liya!!
43 Hello Dhinchak Pooja!         Selfie mayne la liya!!
44 Hello Dhinchak Pooja!         Selfie mayne la liya!!
45 Hello Dhinchak Pooja!         Selfie mayne la liya!!
46 Hello Dhinchak Pooja!         Selfie mayne la liya!!
47 Hello Dhinchak Pooja!         Selfie mayne la liya!!
48 Hello Dhinchak Pooja!         Selfie mayne la liya!!
49 Hello Dhinchak Pooja!         Selfie mayne la liya!!
50 Hello Dhinchak Pooja!         Selfie mayne la liya!!
51 Hello Dhinchak Pooja!         Selfie mayne la liya!!
52 Hello Dhinchak Pooja!         Selfie mayne la liya!!
53 Hello Dhinchak Pooja!         Selfie mayne la liya!!
54 Hello Dhinchak Pooja!         Selfie mayne la liya!!
55 Hello Dhinchak Pooja!         Selfie mayne la liya!!
56 Hello Dhinchak Pooja!         Selfie mayne la liya!!
57 Hello Dhinchak Pooja!         Selfie mayne la liya!!
58 Hello Dhinchak Pooja!         Selfie mayne la liya!!
59 Hello Dhinchak Pooja!         Selfie mayne la liya!!
60 Hello Dhinchak Pooja!         Selfie mayne la liya!!
61 Hello Dhinchak Pooja!         Selfie mayne la liya!!
62 Hello Dhinchak Pooja!         Selfie mayne la liya!!
63 Hello Dhinchak Pooja!         Selfie mayne la liya!!
64 Hello Dhinchak Pooja!         Selfie mayne la liya!!
65 Hello Dhinchak Pooja!         Selfie mayne la liya!!
66 Hello Dhinchak Pooja!         Selfie mayne la liya!!
67 Hello Dhinchak Pooja!         Selfie mayne la liya!!
68 Hello Dhinchak Pooja!         Selfie mayne la liya!!
69 Hello Dhinchak Pooja!         Selfie mayne la liya!!
70 Hello Dhinchak Pooja!         Selfie mayne la liya!!
71 Hello Dhinchak Pooja!         Selfie mayne la liya!!
72 Hello Dhinchak Pooja!         Selfie mayne la liya!!
73 Hello Dhinchak Pooja!         Selfie mayne la liya!!
74 Hello Dhinchak Pooja!         Selfie mayne la liya!!
75 Hello Dhinchak Pooja!         Selfie mayne la liya!!
76 Hello Dhinchak Pooja!         Selfie mayne la liya!!
77 Hello Dhinchak Pooja!         Selfie mayne la liya!!
78 Hello Dhinchak Pooja!         Selfie mayne la liya!!
79 Hello Dhinchak Pooja!         Selfie mayne la liya!!
80 Hello Dhinchak Pooja!         Selfie mayne la liya!!
81 Hello Dhinchak Pooja!         Selfie mayne la liya!!
82 Hello Dhinchak Pooja!         Selfie mayne la liya!!
83 Hello Dhinchak Pooja!         Selfie mayne la liya!!
84 Hello Dhinchak Pooja!         Selfie mayne la liya!!
85 Hello Dhinchak Pooja!         Selfie mayne la liya!!
86 Hello Dhinchak Pooja!         Selfie mayne la liya!!
87 Hello Dhinchak Pooja!         Selfie mayne la liya!!
88 Hello Dhinchak Pooja!         Selfie mayne la liya!!
89 Hello Dhinchak Pooja!         Selfie mayne la liya!!
90 Hello Dhinchak Pooja!         Selfie mayne la liya!!
91 Hello Dhinchak Pooja!         Selfie mayne la liya!!
92 Hello Dhinchak Pooja!         Selfie mayne la liya!!
93 Hello Dhinchak Pooja!         Selfie mayne la liya!!
94 Hello Dhinchak Pooja!         Selfie mayne la liya!!
95 Hello Dhinchak Pooja!         Selfie mayne la liya!!
96 Hello Dhinchak Pooja!         Selfie mayne la liya!!
97 Hello Dhinchak Pooja!         Selfie mayne la liya!!
98 Hello Dhinchak Pooja!         Selfie mayne la liya!!
99 Hello Dhinchak Pooja!         Selfie mayne la liya!!
100 Hello Dhinchak Pooja!        Selfie mayne la liya!!
Press any key to continue . . .

Explanation:

The following while loop displays Hello Dhinchak Pooja!     Selfie mayne la liya!! 101 times rather than 100 times. Please see the out it is started form 0 to 100. i.e 101 times. The error lies in the condition, which should be number < 100 rather than number <= 100