while Loop in Java Programming
Loops in Java
In this lesson, we will understand what is while loop in Java programming with some examples.
What is while Loop
while loop is used in situations where we do not know the exact number of iterations of loop beforehand. while loops continue to loop as long as a test expression is true. The while loop is also called an Entry Controlled loop because the test expression is tested before entering the loop body.
Syntax of while Loop
initialization_expression;
while(test_expression)
{
// body of the loop
// statements we want to execute
update_expression;
}
Example
public class Example
{
public static void main(String args[])
{
// initialization expression
int i = 1;
// test expression
while(i<=5)
{
System.out.println("Hello World");
// update expression
i=i+1;
}
}
}
Output
Hello World Hello World Hello World Hello World Hello World
Just like for loop in while loop also, we initialize the loop variable before starting the while loop. After initialization, we will write our test expression inside the parenthesis () of while loop. If the test expression evaluates to true, then the body of the while loop will execute and, the update expression will update the loop value. This process continues until the test expression evaluates to false. When the test expression evaluates to false the while loop terminates.
Example 1
Java program to print the numbers from 1 to 10 on the screen using while loop.
public class Example
{
public static void main(String args[])
{
int i=1;
while(i<=10)
{
System.out.println(i);
i=i+1;
}
}
}
Output
1 2 3 4 5 6 7 8 9 10
In the above example, we have run a while loop from 1 to 10. Each time when the loop runs, we print the value of the variable i on the screen on a separate line using System.out.println() statement. The loop ends when the value of i is more than 10.
Example 2
Java program to print all the even numbers from 10 to 20 on the screen using while loop.
public class Example
{
public static void main(String args[])
{
int i=10;
while(i<=20)
{
System.out.println(i);
i=i+2;
}
}
}
Output
10 12 14 16 18 20
In the above example, we have run a while loop from 10 to 20. Each time when the loop runs, we print the value of the variable i on the screen on a separate line using System.out.println() statement and then increment the value of i by 2. The loop ends when the value of i is more than 20.
Example 3
Java program to print all the numbers from 10 to 1 in reverse order on the screen using while loop.
public class Example
{
public static void main(String args[])
{
int i=10;
while(i>=1)
{
System.out.println(i);
i=i-1;
}
}
}
Output
10 9 8 7 6 5 4 3 2 1
In the above example, we have run a while loop from 10 to 1 in reverse order. Each time when the loop runs, we print the value of the variable i on the screen on a separate line using System.out.println() statement and then decrement the value of i by 1. The loop ends when the value of i is less than 1.
Example 4
Java program to input a 4-digit number and find the sum of its digits using while loop.
import java.util.Scanner;
public class Example
{
public static void main(String args[])
{
int n,r,sd=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a 4 digit number ");
n=sc.nextInt();
if(n>=1000 && n<=9999)
{
while(n>0)
{
r=n%10; // return the last digit of the number as remainder
sd=sd+r;
n=n/10; // removes the last digit of the number
}
System.out.println("Sum of digits = " + sd);
}
else
{
System.out.println("Not a 4 digit number");
}
}
}
Output
Enter a 4 digit number 2461 Sum of digits = 13
In the above example, we have input a 4-digit number. The loop starts with the 4-digit number and runs till the value of n is greater than 0. Each time when the loop runs, we find the last digit of the number using n%10 and add the digit in the variable sd and remove the last digit from the number using n=n/10. The loop ends when the value of n becomes 0 after removing all the digits from n.
Nested while Loop
Java programming allows using one while loop inside another while loop. This is known as nested while loop.
Syntax of Nested while Loop
initialization_expression;
while(test_expression)
{
// body of the outer loop
// statements we want to execute
initialization_expression;
while(test_expression)
{
// body of the inner loop
// statements we want to execute
update_expression;
}
update_expression;
}
Example
public class Example
{
public static void main(String args[])
{
int i=1,j;
while(i<=5)
{
j=1;
while(j<=i)
{
System.out.print(j);
j++; // it means j=j+1
}
System.out.println();
i++; // it means i=i+1
}
}
}
Output
1 12 123 1234 12345
In the above example, we have run two loops, one outer loop and another inner loop. The outer loop runs from 1 to 5, and the inner loop runs from 1 to the current value of the outer loop (for example, when the value of i is 1, the inner loop runs from 1 to 1. When the value of i is 2, the inner loop runs from 1 to 2 and so on). The program ends when the outer loop test expression evaluates to false.
Infinite while Loop
An infinite while loop is a loop that never ends because its test condition never evaluates to false. It keeps on executing unless and until we terminate the loop using the break statement as shown in the example below.
Syntax of Infinite while Loop in Java
while(true)
{
// body of the infinite while loop
}
Example
public class Example
{
public static void main(String args[])
{
int i=0;
while(true)
{
System.out.println("Hello World");
i=i+1;
if(i==5)
{
break; // terminate the infinite loop when the value of i is 5
}
}
}
}
Output
Hello World Hello World Hello World Hello World Hello World
Test Your Knowledge
Attempt the practical questions to check if the lesson is adequately clear to you.