Contents
Syntax
The while
true
. Its syntax can be expressed as:
while (expression) { statement(s) }
The while
statement evaluates expression, which must return a boolean
value. If the expression evaluates to true
, the while
statement executes the statement(s) in the while
block. The while
statement continues testing the expression and executing its block until the expression evaluates to false
.
Example
Using the while
statement to print the values from 1 through 10 can be accomplished as in the following WhileDemo
program:
class WhileDemo { public static void main(String[] args){ int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++; } } }
Define An Infinite Loop Using While Loop
You can implement an infinite loop using the while
statement as follows:
while (true){ // your code goes here }