A while loop is Java’s most fundamental looping statement. It repeats the block of code written inside the loop till the controlling statement is true. The controlling statement can be any boolean expression. Once the condition becomes false, the control gets out of the loop and starts executing the next line of code immediately following the loop.
A while loop is called an entry controlled loop. That means every time while loop checks the condition before executing its body.
Table of Contents
How to write while loop in Java?
The structure of the while loop is very simple. Java while loop syntax is given below.
Syntax
while(condition)
{
Statement1;
Statement2;
.
.
Statementn;
}
For a single statement, curly brackets are not necessary. Also, we can write nested while loops.
How to use while loop in Java?
The while loop works the same as for loop. Only the change is. In for loop, we initialize a variable check for loop’s controlling statements and increment the variable’s value in the header part in the loop’s brackets. But in the while loop, we only write the controlling statement in the header part. Sometimes the loop executes infinitely because we cannot specify the number of iterations. To avoid this, we need to keep a counter that continuously increments its value until the condition gets false. Let’s understand this with an example.
How to stop a while loop in Java?
To stop the while loop, Java provides a break statement. After the specific condition satisfies, if we write the break statement, the control will get immediately out of the loop. The break statement is used to break the flow of the loop. We can use break statements in all types of the loop like for loop, while loop, do-while loop in Java.
Java while loop examples
We can use Java while loop in many programs. We can say while loop is a substitute of for loop.
1. Simple code of while loop in Java
This is a simple example of printing the 1 to 10 with a while loop. Store 1 number in a variable. Check condition in the while loop and print the number. After that, increment the value of the variable by 1. This procedure will execute until the condition gets false.
Example
public class while_loop {
public static void main(String[] args) {
int i = 1;
while(i<=10)
{
System.out.println(i);
i++;
}
}
}
Output
1
2
3
4
5
6
7
8
9
10
2. Infinite while loop in Java
Writing true in the controlling statement will execute the loop infinitely.
Example
public class while_loop {
public static void main(String[] args) {
int i = 1;
while(true)
{
System.out.println(i);
i++;
}
}
}
The above example will print numbers from 1 to infinity.
3. Printing table using while loop in Java
We have accepted the number from the user and printed its table by multiplying the number with 1,2,3, and so on till 10. Printing its result will give a table of the entered number.
Example
import Java.util.Scanner;
public class while_loop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number:");
int n = sc.nextInt();
int i = 1;
System.out.println("Table of "+n+" is :");
while(i<=10)
{
System.out.println(n*i);
i++;
}
}
}
Output
Enter number:5
Table of 5 is :
5
10
15
20
25
30
35
40
45
50
4. Printing pattern using nested while loop in Java
Exmaple
public class while_loop {
public static void main(String[] args) {
int i = 0;
while(i<5)
{
int j=0;
while(j<=i) // nested while loop
{
System.out.print("* ");
j++;
}
i++;
System.out.println("\n");
}
}
}
Output
*
* *
* * *
* * * *
* * * * *
5. Sum of n natural numbers using while loop in Java
Accept a range of natural numbers from the user. Iterate the while loop for that range. Store an addition of all the numbers in a variable and print it.
Example
import Java.util.Scanner;
public class while_loop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Lower bound of the range :");
int n1 = sc.nextInt();
System.out.println("Enter upper bound of the range");
int n2 = sc.nextInt();
int sum = 0;
if(n2>n1)
{
while(n1<=n2)
{
sum = sum + n1;
n1++;
}
System.out.println("Sum is :"+sum);
}
else
{
System.out.println("Lower bound is greater than upper bound");
}
}
}
Output
Enter Lower bound of the range :
10
Enter upper bound of the range
100
Sum is :5005
6. While loop program using break statement in Java.
Iterate while loop and write a break statement after satisfying specific conditions.
Example
public class while_loop {
public static void main(String[] args) {
int i = 10;
while ( i<=100)
{
if(i==50)
break; // break statement
System.out.println(i);
i = i+10;
}
}
}
Output
10
20
30
40
To break out of a while loop, we can use a break statement. The break statement is used to break the flow of the loop.