While Loop in Java Program
Java While Loop
In Java, a while loop is used to iterate a part of the program continued until a specified condition is true. The second the boolean condition becomes false, the loop automatically stops. It is recommended to use a while loop in the java program when a number of iterations is not fixed. It allows you to write fewer lines of code and enhances code readability.
Syntax:
initialization expression while(condition){ //code to be executed update expression }
Different Parts of while loop:
-
- initialization expression: It contains the value on which loop iterations depend.
- Condition: It is an expression that includes initialization expression with operators and operands and evaluates to either of the boolean values which are true and false. The loop iterates as long as the condition is true. Program control exits the loop when the condition becomes false.
- update expression: It changes the value of the initialization expression. Usually, the increment and decrement operators are used. But, you can use any of the operators depending upon your program requirements.
Flowchart:
Java While Loop Example:
//Java program with while loop public class WhileLoop { public static void main(String[] args){ System.out.println("Numbers from 1 to 10 that are divisible by 3."); //loop variable int i=1; while(i<=10){ if(i%3==0){ //executes when if-condition is true System.out.println(i); } //updates loop variable i i++; } } }
In the program provided above, a while loop has been used that displays numbers from 1 to 10 only if they are divisible by 3. Test of divisibility by 3 has been specifies using if-statement.
Output:
Numbers from 1 to 10 that are divisible by 3. 3 6 9
Java Infinitive While Loop
It is a while loop that runs infinitely. It is possible when the while-condition never becomes false.
Syntax:
while(true){ //code }
Factorial Program in Java using While Loop
//Java program to find factorial of a number using while loop public class FactorialWhile { public static void main(String[] args){ //number of which factorial needs to be found int number=4; //stores the factorial of "number" int factorial=1; //loop variable initialized to "number" int i=number; while(i>1){ //main logic that calculates the factorial factorial*=i; i--; } System.out.println("Factorial of "+number+" is "+factorial); } }
In the program provided above, the factorial of “number” is being calculated using a while loop. The variable “number” has been initialized to 4.
Output:
Factorial of 4 is 24
Java Do While Loop
It is slightly different from the while loop as it allows the loop body to execute no matter what the boolean condition evaluates to. This means that do-while loop statements execute at least once in the program.
Syntax:
initialization expression do{ //code update expression }while(condition);
Note:
The do-while loop ends with a semicolon after the while condition.
Flowchart:
Java Do-While Loop Example:
//Java program with do-while loop public class DoWhileLoop { public static void main(String[] args){ System.out.println("Numbers from 1 to 10 that are divisible by 3."); //loop variable int i=1; do{ if(i%3==0){ //executes when if-condition is true System.out.println(i); } //updates loop variable i i++; }while(i<=10); } }
In the program provided above, a do-while loop has been used that displays numbers from 1 to 10 only if they are divisible by 3. Test of divisibility by 3 has been specified using if-statement.
Output:
Numbers from 1 to 10 that are divisible by 3. 3 6 9