R语言 循环语句、分支语句和中止语句-控制流篇 for 循环 while 循环 repeat 循环 if/else 判断 中止语句

用法

for (n in m) expr 

若n在m中则运行 expr

while 循环

用法

while (condition) expr

当符合condition时运行expr

repeat 循环

用法

repeat expr

持续运行expr。通常配合break中止语句使用,不然将陷入无限循环。

if/else 判断

用法

if (condition1)
    expr1
else if (condition2)
    expr2
else 
    expr3 

当符合condition1时运行expr1,不符合1但符合2时运行expr2,都不符合时运行expr3。

中止语句

用法

break #中止循环退出循环
next #中止当前循环继续循环