Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java While Loop Program

In this tutorial, we will learn how to implement a while loop in different scenarios. But before moving further, if you are not familiar with the concept of while loop, then do check the article on Loops in Java.

Syntax While Loop

while(condition)
{
   //Code to be executed
}

Program 1: Java Program to Implement While Loop

In this program, we will see how to implement a while loop program in java. Here, we will consider a scenario where we will find the multiplication table of a particular number. Instead of writing the multiplication table for each element, we will use a while loop for the same. We will write the statement once and it will be implemented multiple times.

Algorithm

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a number

  4. Ask the user to initialize the number.

  5. Use a while loop to print the multiplication table of that number.

  6. Display the result.

  7. Stop.

Below is the Java code of the while loop.

//Java Program to see the implementation while loop program
import java.util.*;

public class Main
{
     public static void main(String []args)
     {
        //Take input from the user
        //Create instance of the Scanner Class
        Scanner sc=new Scanner(System.in); 
        System.out.println("Enter the number: ");
        int n=sc.nextInt();      //Declare and initialize the number
        int i=1;
        System.out.println("The multiplication table of "+n+" is: ");
        //Infinite Loop Example      
        while(i<=10)
        {
           System.out.println(n+" * "+i+" = "+ (n*i));
            i++;
        }  
     }
}


Enter the number: 3
The multiplication table of 3 is:
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30

Program 2: Java Program to Implement While Loop

In this program, how to find the sum of all the entered positive numbers using a while loop.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a variable.

  4. Ask the user to initialize the variable.

  5. Declare another variable to store the sum of all the positive numbers.

  6. Initialize it to 0.

  7. Use a while loop to check whether the entered numbers are positive or not.

  8. Increment the sum each time a positive number is entered.

  9. Break the loop if any negative number is entered.

  10. Display the sum.

  11. Stop.

Below is the Java code of the while loop.

//Java Program to calculate the sum of entered positive numbers using a while loop
import java.util.*;

public class Main
{
     public static void main(String []args)
     {
        //Take input from the user
        //Create instance of the Scanner Class
        Scanner sc=new Scanner(System.in); 
        System.out.println("Enter the number: ");
        int n=sc.nextInt();      //Declare and initialize the number
        int sum=0;
        //While loop will take values only if the numbers are positive
        while (n >= 0) 
        {
            // add only positive numbers
            sum += n;
            System.out.println("Enter the number:");
            n = sc.nextInt();
        }
        System.out.println("Sum of the entered positive numbers = " + sum);
        
     }
}


Enter the number: 3
Enter the number: 4
Enter the number: 3
Enter the number: 2
Enter the number: 1
Enter the number: -9
Sum of the entered positive numbers = 13

Program 3: Java Program to Implement While Loop

In this program, we will see how to use a while loop to perform a certain task infinite times. In order to do so, we will pass true in the condition statement of the while loop. Doing so will make it an infinitive while loop. A point to be noted here is that, in order to exit from an infinite loop, you need to press ctrl+c.

Algorithm:

  1. Start

  2. Declare a variable.

  3. Initialize it to 1.

  4. Pass true in the condition of the while loop.

  5. Execute the statement until the condition is false.

  6. Increment the variable in each iteration.

  7. Display the result.

  8. Stop.

Below is the Java code of the while loop.

//Java Program for the implementation of a while loop
public class Main
{
     public static void main(String []args)
     {
        int i=1;
        //If true is passed in a while loop, then it will be infinitive while loop.
        while (true) 
        {
            System.out.println(i + " Hello World!");
           i++;
        }  
     }
}


1 Hello World!
2 Hello World!
3 Hello World!
4 Hello World!
5 Hello World!
6 Hello World!
7 Hello World!
ctrl+c