Java While Loop

 

It is the simplest loop of Java. This loop executes one or more statements while the given condition remains true. It is useful where the number of iterations are not known in advance.

SYNTAX:

While (condition)

         {

Statements;

}

Condition: the condition is given relational expression. It controls the iteration of loop. The statements are executed only if the condition is true.

Statement: it is the instruction that is executed when the condition is true. Two or more statements are specified in braces {}. It is called the body of the loop.

 

Working of While loop:

First of all, the condition is evaluated. If it is true, the control enters the body of the loop and executes all statements in the body. After executing the statements it again moves to the start of loop and check the condition. This process continues as long as the condition remains true. When the condition becomes false, the loop is terminated. While loop terminates only when the condition becomes false if the condition remains true, the loop never ends. A loop that has no end point is known as infinite loop.

 

Flowchart:

flowchart

Example of while loop in Java:

Consider the following example in which we have used the while loop to print a string of characters:

CODE:

public class ExampleWhile {

public static void main (string [] args) {

int n;

n =1;

While (n<=5)

{

system. out. println (“Country”);

n++;

}

}

}

OUTPUT:

Country

Country

Country

Country

Country

In the above example while loop is used to print “Country” five times. We declared a variable n and initialized it at 1. The condition of the “while” loop is checked, the condition is true because at this time the value of the variable n is 1 and the condition is that n should be smaller than or equal to 5. The control enters the body of the loop and the message is printed then there is a statement in which the value of n is incremented by 1. Now the values of the variable n is 2, again the condition is checked and if the condition is true the control will enter the body of the loop and so on.

 

Java infinitive while loop:

If in the “while” loop true is passed by the user then the loop becomes infinitive in Java. The following is the syntax of the infinitive “while” loop in Java programming language:

 

Syntax:

while (true) {

statements;

}

Example of infinitive while loop in Java:

Consider the following example in which we have used the infinitive while loop by passing true:

CODE:

public class ExampleWhile {

public static void main (string [] args) {

while (true) {

system. out. println (“Infinitive While loop in Java”);

}

}

}

OUTPUT:

Infinitive While loop in Java

Infinitive While loop in Java

Infinitive While loop in Java

Infinitive While loop in Java

Infinitive While loop in Java

Infinitive While loop in Java

Infinitive While loop in Java

Infinitive While loop in Java

Infinitive While loop in Java

Infinitive While loop in Java

Infinitive While loop in Java

Infinitive While loop in Java

The loop will not break until the user press ctrl + c. The combination that is ctrl + c is used to exit from the loop or program.