JAVA WHILE LOOP

A while loop in java is a way to write a looping structure which can iterate over a specified block multiple times as long as a specified condition is true.

Table of Contents

[hide]
  • While Loop in Java
  • Example :Factorial using while loop
  • Example :Fibonacci Series using while loop

While Loop in Java

Loops in Java Programming Language is a way to efficiently write a code that will iterate over a part of block multiple times if the condition is true.

While loop is preffered over for loop if we know the number of iterations from before. While loop is also same as for loop, it can do all the necessary operations same as for loop.

Just take an example that you want to print your name on the screen 200 times, for printing something we use System.out.print, in this case, we have to use this 200 times to print the name which is not recommended. For this purpose, we use loops in which we write the print statement only once and it can run multiple times until certain condition is satisfied.

Java While loop Syntax

while(conditional expression) {// Code to be executed }

Working :

In the above expression while loop accepts a conditional expression as a parameter that always evaluates to a boolean value i.e (true or false). If the expression returns true then while loop is executed else in case of false while loop in terminated. Expression is evaluated again and again until the condition is false.

Flow Diagram

java while loop

While working with a while loop, the increment, and decrement of loop variables happen inside the while loop so that values get changed on each iteration and we could get false at some point.

Example : Factorial using while loop

We have already solved the factorial problem using for loop. If not yet then go through Java For Loop Examples. Here we are going to solve the same problem with the help of while loop so that you can get the basic understanding how for and while loop works.

copyimage
// Java program to calculate factorial of any number
import java.util.*;
    public class FactorialExample{
         public static void main(String args[]){  
             int fact = 1, i = 1;
             Scanner sc= new Scanner(System.in); 
             System.out.print("Enter any number : ");
             int num = sc.nextInt(); 

             while(i <= num) {
             fact = fact * num;
             num--;
         }
        System.out.println( "Factorial is : " + fact);
    }
}
Enter any number : 8

OUTPUT :

Factorial is : 40320

Dry Run

InitializationCondition checkinside Loop bodyIncr ++ /Decr --
i=1 ; / fact =1; / num = 61<=6; // truefact= 1 * 6 = 6num-- = 5
i=1 ; / fact =6; / num = 51<=5; // truefact= 6 * 5 = 30num-- = 4
i=1 ; / fact =30; / num = 41<=4; // truefact= 30 * 4 = 120num-- = 3
i=1 ; / fact =120; / num = 31<=3; // truefact= 120 * 3 = 360num-- = 2
i=1 ; / fact =360; / num = 21<=2; // truefact= 360* 2 = 720num-- = 1
i=1 ; / fact =360; / num = 11<=1; // truefact= 720* 1 = 720num-- = 0
i=1 ; / fact =720; / num = 01<=0; // false

We are getting the correct answer while dry run, hence we are sure that our solution is correct. Kindly take other inputs apart from 6 and dry run to check the solution.

Example : Fibonacci Series using while loop

Fibonacci Series in Java is a series of numbers where the number is the sum of the next two terms . The first two numbers/terms are 0 followed by 1 and so on.

Assume you have to find the Fibonacci series of a number say 8, then Fibonacci series be calculated as 8 = 0 , 1 , 1 (0+1), 2 (1+1), 3 (1+2), 5 (2+3), 8 (3+5), 13 (5+8).

copyimage
// Java program to generate fibonacci series of any number
import java.util.*;
     public class FibonnaciExample{
         public static void main(String args[]){  
         int i = 1 , num1 = 0, num2 = 1, result = 0;
         Scanner sc= new Scanner(System.in); 
         System.out.print("Enter any number to generate fibonacci series : ");
         int num = sc.nextInt(); 

         while(i <= num) {
         System.out.print(num1 + " ");
         result = num1 + num2;
         num1 = num2;
         num2 = result;
         i++;
      }
   }
}
Enter any number to generate fibonacci series : 8

OUTPUT :

0 1 1 2 3 5 8 13

Dry Run

initializationCondition checkInside loopAssignmentsIncr ++ / Decr --
i=1,num1=0,num2=1,result=01<=8 // trueprint- 0result= 1,num1=1,num2=1i++ = 2
i=2,num1=1,num2=1,result=12<=8 // trueprint- 1result= 2,num1=1,num2=2i++ = 3
i=3,num1=1,num2=2,result=23<=8 // trueprint- 1result= 3,num1=2,num2=3i++ = 4
i=4,num1=2,num2=3,result=34<=8 // trueprint- 2result= 5,num1=3,num2=5i++ = 5
i=5,num1=3,num2=5,result=55<=8 // trueprint- 3result= 8,num1=5,num2=8i++ = 6
i=6,num1=5,num2=8,result=86<=8 // trueprint- 5result= 13,num1=8,num2=13i++ = 7
i=7,num1=8,num2=13,result=137<=8 // trueprint- 8result= 21,num1=13,num2=21i++ = 8
i=8,num1=13,num2=21,result=218<=8 // trueprint- 13result= 34,num1=21,num2=34i++ = 9
i=9,num1=21,num2=34,result=349<=8 // false

Kindly take other inputs apart from 8 and dry run to check the solution. Now its time to learn last loop of Java control statements do-while loop in Java.