The while loop in Java is used to repeatedly execute a block of code when a condition is true. Usage includes: initialize condition, check condition, execute code block, update state, repeat steps 2-4 until condition becomes false. For example, to print numbers from 1 to 10 you can use a while loop: int i = 1; while (i <= 10) { System.out.println(i); i ; }
Usage of while loop in Java
The while loop is a control flow statement in Java that is used to execute a block of code when a given condition is true. The syntax is as follows:
<code>while (condition) { // 代码块 }</code>
where:
condition
is a Boolean expression that evaluates the condition. Code block
is the code to be executed. Usage:
Example:
Print numbers from 1 to 10:
<code class="java">int i = 1; while (i <= 10) { System.out.println(i); i++; }</code>
Points to note:
break
statement to force out of the loop. continue
statement to skip the remainder of the current loop iteration. The above is the detailed content of How to use while loop in java. For more information, please follow other related articles on the PHP Chinese website!