r语言循环控制语句
The main usage of these loop control statements is where the programmers would like to change the order of execution in his or her program. Here, using these loop control statements whenever declared in a particular scope then after the execution of that particular statement then the execution comes out of that scope. Now whatever the objects created in that scope will not be executed once the execution comes out of that particular scope area. Thus, all such objects will be destroyed. The R language mostly supports the usage of the following control statements.
这些循环控制语句的主要用法是程序员希望更改其程序中执行顺序的地方。 在这里,只要在特定范围内声明这些语句,就使用这些循环控制语句,然后在执行该特定语句之后,该执行就会超出该范围。 现在,一旦执行脱离了该特定范围区域,则在该范围中创建的任何对象都将不会执行。 因此,所有这些物体将被破坏。 R语言主要支持以下控制语句的用法。
中断声明 (The break statement)The break statement terminates the loop and finally transfers the execution to the subsequent statement that is after the loop. In addition, one can use this break statement inside the else statement or if….else branching statements. Also, it is used for the termination of the case in a switch statement.
break语句终止循环,最后将执行转移到循环之后的后续语句。 此外,可以在else语句或if .... else分支语句中使用此break语句。 此外,它还用于在switch语句中终止案例。
Syntax:
句法:
break
Usage of break statement in the for loop:
for循环中break语句的用法:
numbers <- 50:62
for (i in numbers) {
if (i==60){
break
}
print(i)
}
Output
输出量
[1] 50 [1] 51 [1] 52 [1] 53 [1] 54 [1] 55 [1] 56 [1] 57 [1] 58 [1] 59下一条语句 (The next statement)
The next statement emulates the behavior of the switch statement in R. This particular statement is widely employed in the codes where the for loop and while loop is largely used. However, this next statement can also make use in the else branch of the if...else statements in the R language.
下一条语句模拟R中switch语句的行为。该特定语句广泛用于大量使用for循环和while循环的代码中。 但是,此下一条语句也可以在R语言的if ... else语句的else分支中使用。
Syntax:
句法:
next
The next statement terminates the present iteration of the loop where it is included and thus helps the loop in jumping to the next iteration.
下一条语句终止包含它的循环的当前迭代,因此有助于循环跳转到下一个迭代。
Usage of the next statement in for loop:
for循环中下一条语句的用法:
# Eliminating even numbers using next statement
vector <- c(2,43, 1,12,19)
ct <- 0
for (v in vector) {
if((v%%2) == 0)
next
print(v)
}
Output
输出量
[1] 43 [1] 1 [1] 19
翻译自: https://www.includehelp.com/r/loop-control-statements-break-and-next.aspx
r语言循环控制语句



