Skip to Content

While Loop

While loop executes group of Java statements as long as the boolean condition evaluates to true.

While loop syntax

  1. while(<boolean condition>){
  2.  
  3.         <Block of statements>;
  4.  
  5. }

Block of statements is any valid Java code. Boolean condition is any valid Java expression that evaluates to boolean value. Braces are options if there is only one statement to be executed.

While loop evaluates the boolean expression, and if it is true, it executes the block of statements. It continuously executes the block of statements until the boolean condition evaluates to false value. After that loop is terminated and statement immediately after the loop body will be executed.

While Loop Examples