在 R 中的 while 循环中嵌套多个 if 语句
Nesting multiple if statements inside a while loop in R
是否可以在 while 循环中嵌套多个 if 语句?
我正在尝试创建一个简单的示例来向他们展示自己:
i <- 1
while(i <=10) {
if(i > 6){
cat("i=",i,"and is bigger than 6.\n")
}else{if(3<i & i<6){
cat("i=",i,"and is between 3 and 6.\n")
}else{
cat("i=",i,"and is 3 or less.\n")
}
i<-i+1
cat("At the bottom of the loop i is now =",i,"\n")
}
我的示例代码一直卡在 i=7 并希望永远 运行。我怎样才能避免这种情况?
在第一个 else
之后,您还有一个额外的 {
i <- 1
while(i <=10) {
if(i > 6){
cat("i=",i,"and is bigger than 6.\n")
}else if(3<i & i<6){
cat("i=",i,"and is between 3 and 6.\n")
}else{
cat("i=",i,"and is 3 or less.\n")
}
i<-i+1
cat("At the bottom of the loop i is now =",i,"\n")
}
如@Alex P 所述,您有一个额外的 {
。
不过,您也可以通过检查 i
是否大于等于 3
来简化 else if
(您已经知道 i
将小于或等于6 从它失败第一个 if
条件你检查 i > 6
):
i <- 1
while(i <=10) {
if(i > 6) {
cat("i =", i, "and is bigger than 6.\n")
} else if(i >= 3) {
cat("i =", i ,"and is between 3 and 6 inclusive.\n")
} else {
cat("i =", i ,"and is less than 3.\n")
}
i = i + 1
cat("At the bottom of the loop i is now =", i ,"\n")
}
输出:
i = 1 and is less than 3.
At the bottom of the loop i is now = 2
i = 2 and is less than 3.
At the bottom of the loop i is now = 3
i = 3 and is between 3 and 6 inclusive.
At the bottom of the loop i is now = 4
i = 4 and is between 3 and 6 inclusive.
At the bottom of the loop i is now = 5
i = 5 and is between 3 and 6 inclusive.
At the bottom of the loop i is now = 6
i = 6 and is between 3 and 6 inclusive.
At the bottom of the loop i is now = 7
i = 7 and is bigger than 6.
At the bottom of the loop i is now = 8
i = 8 and is bigger than 6.
At the bottom of the loop i is now = 9
i = 9 and is bigger than 6.
At the bottom of the loop i is now = 10
i = 10 and is bigger than 6.
At the bottom of the loop i is now = 11
是否可以在 while 循环中嵌套多个 if 语句?
我正在尝试创建一个简单的示例来向他们展示自己:
i <- 1
while(i <=10) {
if(i > 6){
cat("i=",i,"and is bigger than 6.\n")
}else{if(3<i & i<6){
cat("i=",i,"and is between 3 and 6.\n")
}else{
cat("i=",i,"and is 3 or less.\n")
}
i<-i+1
cat("At the bottom of the loop i is now =",i,"\n")
}
我的示例代码一直卡在 i=7 并希望永远 运行。我怎样才能避免这种情况?
在第一个 else
{
i <- 1
while(i <=10) {
if(i > 6){
cat("i=",i,"and is bigger than 6.\n")
}else if(3<i & i<6){
cat("i=",i,"and is between 3 and 6.\n")
}else{
cat("i=",i,"and is 3 or less.\n")
}
i<-i+1
cat("At the bottom of the loop i is now =",i,"\n")
}
如@Alex P 所述,您有一个额外的 {
。
不过,您也可以通过检查 i
是否大于等于 3
来简化 else if
(您已经知道 i
将小于或等于6 从它失败第一个 if
条件你检查 i > 6
):
i <- 1
while(i <=10) {
if(i > 6) {
cat("i =", i, "and is bigger than 6.\n")
} else if(i >= 3) {
cat("i =", i ,"and is between 3 and 6 inclusive.\n")
} else {
cat("i =", i ,"and is less than 3.\n")
}
i = i + 1
cat("At the bottom of the loop i is now =", i ,"\n")
}
输出:
i = 1 and is less than 3.
At the bottom of the loop i is now = 2
i = 2 and is less than 3.
At the bottom of the loop i is now = 3
i = 3 and is between 3 and 6 inclusive.
At the bottom of the loop i is now = 4
i = 4 and is between 3 and 6 inclusive.
At the bottom of the loop i is now = 5
i = 5 and is between 3 and 6 inclusive.
At the bottom of the loop i is now = 6
i = 6 and is between 3 and 6 inclusive.
At the bottom of the loop i is now = 7
i = 7 and is bigger than 6.
At the bottom of the loop i is now = 8
i = 8 and is bigger than 6.
At the bottom of the loop i is now = 9
i = 9 and is bigger than 6.
At the bottom of the loop i is now = 10
i = 10 and is bigger than 6.
At the bottom of the loop i is now = 11