在许多语言中,有一条名为的指令break
告诉解释器在当前语句之后退出 switch。如果省略它,则在处理当前案例后切换:
switch (current_step)
{
case 1:
print("Processing the first step...");
# [...]
case 2:
print("Processing the second step...");
# [...]
case 3:
print("Processing the third step...");
# [...]
break;
case 4:
print("All steps have already been processed!");
break;
}
如果您想通过一系列传递条件,这样的设计模式会很有用。
我知道如果程序员忘记插入 break 语句,这可能会由于无意的失败而导致错误,但是默认情况下有几种语言会中断,并包含一个失败关键字(例如continue
在 Perl 中)。
根据设计,R 开关也会在每个案例结束时默认中断:
switch(current_step,
{
print("Processing the first step...")
},
{
print("Processing the second step...")
},
{
print("Processing the third step...")
},
{
print("All steps have already been processed!")
}
)
在上述代码中,如果current_step
设置为 1,则输出仅为"Processing the first step..."
.
R中是否有任何方法可以强制开关盒通过以下情况?