We will discuss in this tutorial about While loop in Java programming language
Loops in Java
In Java, there are three types of loops:
While loop in Java programming language
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.
The syntax of the while loop
while(Test_expression) {// //Body of loop //statement(s) }
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
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"); }
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
Java While keyword
C++ wile keyword
C while keyword
Python while keyword
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…