Java While Loop

18-Sep-2024

Understand how to use the while loop to repeat tasks based on conditions in Java

The while loop in Java is a control flow statement that, if a given condition is met, continually runs a block of code. 


Syntax :



while(condition){
// statements
}


First, while loop condition will tested. If the condition is true then the loop body code will be executed and again test the condition then again execute the loop body code. This process is repeated until the condition is false.


Flow Chart:



 Example:


/**
 * Java While Loop Example
 */
public class JavaExample {

    public static void main(String[] args) {
       
 
    int i = 1;
      while (i<=5) {
        System.out.println(" While Loop Work Here : " +i);
        i++;
      }
    }
}


Output:



While Loop Work Here : 1
While Loop Work Here : 2
While Loop Work Here : 3
While Loop Work Here : 4
While Loop Work Here : 5



Comments