While loop

While loop in Java programming language

While loop in Java programming language

We will discuss in this tutorial about While loop in Java programming language

Loops in Java

In Java, there are three types of loops:

  • for loop
  • while loop
  • do-while loop
While loop in Java

While loop in Java programming language

While loop

Java while loop is used to execute statement (s) repeatedly until the particular condition is satisfied. Initially, the test expression is evaluated and if the outcome is true then, the statements inside the while loop(loop body) are executed. Whereas, when the test expression returns false, the flow of control comes out of the loop or exits from the while loop and goes to the next statements after while loop.

Declaration

The syntax of the while loop

while(Test_expression)
{//
//Body of loop
//statement(s)
}

Flow diagram for while loop

Flow diagram – while loop

In the while loop, first test expression is evaluated.  If it returns true, statements execute inside the while loop but if test expression returns false the flow of control skip the execution and goes out of the loop body.

Example

Program 1

class While_java{
public static void main(String args[]){
int x=0;
while(x<=10){

System.out.println("X is :"+x);
x++;
            }

        }
    }

 

When the above code is executed, it produces the following results:

x is :0
x is :1
x is :2
x is :3
x is :4
x is :5
x is :6
x is :7
x is :8
x is :9
x is :10

 

This is a very simple program for beginners print 1 to 10 natural number(Positive) using while loop in Java

In the above program

initially, x=0(already assigned) text expression x<=10 is true and display 0, then i is updated 1

Now, x=1, text expression x<=10 is true and display 1, then i is updated 2

then, x=2, text expression x<=10 is true and display 2, then i is updated 3

then, x=3, text expression x<=10 is true and display 3, then i is updated 4

then, x=4, text expression x<=10 is true and display 4, then i is updated 5

then, x=5, text expression x<=10 is true and display 5, then i is updated 6

then, x=6, text expression x<=10 is true and display 6, then i is updated 7

then, x=7, text expression x<=10 is true and display 7, then i is updated 8

then, x=8, text expression x<=10 is true and display 8, then i is updated 9

then, x=9, text expression x<=10 is true and display 9, then i is updated 10

then, x=10, text expression x<=10 is true and display 10, then i is updated 11

finally, x=11, text expression x<=10 is false and while loop terminated flow of control and skip from the loop

Program 2

To calculate the total value of 1 to 200 positive numbers

class While_tot{
public static void main(String args[]){
int i=1,sum=0;
while(i<=200){
    sum+=i;//equal to sum=sum+1

//calculate total of 1 to 200 positive numbers
i++;
            }
    System.out.println("Total value is :"+sum);

        }
    }

When the above code is executed, it produces the following results:

Total value is :20100

 

The infinitive while loop in Java

Never ending this while loop in Java

while(true) 
{
//Body of while loop 
//statement(s) 
}

 Program 1

class While_java1{
public static void main(String args[]){
int i=10;
while(i>=1){//unmetered condition

System.out.println("i is :"+i);
i++;
            }

        }//this loop never end
    }

 

When the test expression always true, the loop is never end

When the above code is executed, it produces the following results:

i is : 1
i is : 2
i is : 3
i is : 4
i is : 5
..........
...........

The loop is never ending

press ctrl+c to stop

 

 

Never execute while loop

//the loop body never will be execute
while(false) {
//condition is always false
System.out.print("This will never display");
}

iterating an array element from the array using while loop

We can iterate and printing array element using for loop and while loop

Here, we display array element from single dimension array using while loop

Program 1

class While_array{
public static void main(String args[]){
int arr[]={45,65,78,94,46,35,62};// array declaration
int i=0;//i start in 0 for first array index

while(i<=6){
System.out.println("array length "+(i+1)+" :"+arr[i]);

i++;
            }

        }
    }

 

When the above code is executed, it produces the following results:

Array length 1 :45
Array length 2 :65
Array length 3 :78
Array length 4 :94
Array length 5 :46
Array length 6 :35
Array length 7 :62

 

There are other Java language keywords that are similar to this post

Java While keyword

C++ wile keyword

C while keyword

Python while keyword

Suggested for you

Nested while loop in Java

Nested while loop in Cpp

Nested while loop in C

Nested while loop in Python

 

While loop in Cpp language

While loop in C language

While loop in Python language

 

for loop in Java Language

for loop in C Language

for loop in Python Language

for loop in C++ Language

 

 

 

 

While loop in Python programming language
While loop in Cpp programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago