R 简明教程

R - Loops

有时可能需要多次执行一个代码块。通常,语句以顺序执行。在一个函数中的第一个语句先执行,然后执行第二个语句,依此类推。

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.

循环语句使我们可以多次执行一个语句或一组语句,以下是在大多数编程语言中循环语句的一般形式 −

A loop statement allows us to execute a statement or group of statements multiple times and the following is the general form of a loop statement in most of the programming languages −

loop architecture

R 编程语言提供以下类型的循环来处理循环需要。点击以下链接检查其详细信息。

R programming language provides the following kinds of loop to handle looping requirements. Click the following links to check their detail.

Sr.No.

Loop Type & Description

1

repeat loopExecutes a sequence of statements multiple times and abbreviates the code that manages the loop variable.

2

while loopRepeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.

3

for loopLike a while statement, except that it tests the condition at the end of the loop body.

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.

R 支持以下控制语句。单击以下链接查看其详细信息。

R supports the following control statements. Click the following links to check their detail.

Sr.No.

Control Statement & Description

1

break statementTerminates the loop statement and transfers execution to the statement immediately following the loop.

2

Next statementThe next statement simulates the behavior of R switch.