while Loop in Java


☞ Following is the syntax of while loop.

while(condition){
statement1;
statement2;
………….
}

'while' is a keyword.

☞Whatever you have written in parenthesis is evaluated as true or false.

☞If the condition in the parenthesis is false, control simply skipped while block.

☞If the condition in the parenthesis is true, control enters in while block and execute statements present in the while block. When control reaches at the end of while block it again goes back to while keyword and recheck the condition written in the parenthesis.

☞ If the condition in the parenthesis is again true, control again enters in while block. This keeps on going until condition becomes false.

Example

//Printing 'inspirewebsoft' five times.
public class WhileExample {
public static void main(String[] args) {
    
    int i=1;                //initialization
    while(i<=5) {           //condition
        System.out.println("inspirewebsoft");
        i++;                //flow
    }

}
}

Output

inspirewebsoft
inspirewebsoft
inspirewebsoft
inspirewebsoft
inspirewebsoft