Java 简明教程
Java - Loop Control
When Loops are Required?
您可能需要执行一段代码块多次。通常,语句按顺序执行:函数中的第一个语句先执行,然后是第二个,依此类推。
There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.
编程语言提供了各种控制结构,允许执行更复杂的路径。
Programming languages provide various control structures that allow for more complicated execution paths.
Loop Statement
loop 语句允许我们执行一个语句或语句组多次,以下是大多数编程语言中循环语句的一般形式 −
A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages −
Java Loops
Java 编程语言提供了以下类型的循环来处理循环要求:
Java programming language provides the following types of loops to handle the looping requirements:
Sr.No. |
Loop & Description |
1 |
while loopRepeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. |
2 |
for loopExecute a sequence of statements multiple times and abbreviates the code that manages the loop variable. |
3 |
do…while loopLike a while statement, except that it tests the condition at the end of the loop body. |
4 |
Enhanced for loopAs of Java 5, the enhanced for loop was introduced. This is mainly used to traverse collection of elements including arrays. |
Loop Control Statements
循环控制语句改变了它在正常序列中的执行。当执行退出一个作用域时,在该作用域中创建的所有自动对象会被销毁。
Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
在 Java 中,以下为循环控制语句:
In Java, the following are the loops control statements:
Sr.No. |
Control Statement & Description |
1 |
break statementTerminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch. |
2 |
continue statementCauses the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. |