Thewhilestatement continually executes a block of statements while a particular condition istrue. Its syntax can be expressed as:Thewhile (expression) { statement(s) }whilestatement evaluates expression, which must return abooleanvalue. If the expression evaluates totrue, thewhilestatement executes the statement(s) in thewhileblock. Thewhilestatement continues testing the expression and executing its block until the expression evaluates tofalse. Using thewhilestatement to print the values from 1 through 10 can be accomplished as in the followingWhileDemoprogram:class WhileDemo { public static void main(String[] args){ int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++; } } }You can implement an infinite loop using the
whilestatement as follows:while (true){ // your code goes here }The Java programming language also provides a
do-whilestatement, which can be expressed as follows:The difference betweendo { statement(s) } while (expression);do-whileandwhileis thatdo-whileevaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within thedoblock are always executed at least once, as shown in the followingDoWhileDemoprogram:class DoWhileDemo { public static void main(String[] args){ int count = 1; do { System.out.println("Count is: " + count); count++; } while (count <= 11); } }