Java Loops


A loop is defined as a block of statements, which are repeatedly executed for number times in Java program.


A sequence of statements will be executed after a specified condition is true. This sequence of statements to be executed will be kept inside the flower brackets { } which are known as the Loop body. After every execution of the loop body, the condition will be verified. If it is true the loop body will be executed again. When it is false the loop body will not be executed again.

Java Loop

Three types of Loops in Java programming language:

  • while loop
  • do-while loop
  • for loop

Java While


Java While loop

The while is an entry-controlled loop statement. The test-condition evaluates if the condition is true, then the body of the loop will be executed. This process of execution of repeated until the test condition becomes false and the control is transferred out of the loop.


Syntax:-

while(condition) {
   statement(s); //body of the loop
} 

Example:-

 public class While
{  
	public static void main(String []args) { 
		int i=1;      
		while(i<=5){      
			System.out.println("Print the number " + i );      
			i++;      
		}  
	}
} 



Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.