R 编程中的决策——if、if-else、if-else-if 阶梯、嵌套 if-else 和 switch
决策是关于根据某些条件决定语句的执行顺序。在决策制定中,程序员需要提供一些由程序评估的条件,此外还提供了一些在条件为真时执行的语句,如果条件被评估为假,则可选地执行其他语句。
R中的决策声明如下:
- if 语句
- if-else 语句
- if-else-if 阶梯
- 嵌套 if-else 语句
- 切换语句
if 语句
关键字如果 告诉编译器这是一个决策控制指令,并且关键字 if 后面的条件始终包含在一对括号内。如果条件为 TRUE,则执行语句,如果条件为 FALSE,则不执行语句。
Syntax:
if(condition is true){
execute this statement
}
流程图:
例子:
r
# R program to illustrate
# if statement
a <- 76
b <- 67
# TRUE condition
if(a > b)
{
c <- a - b
print("condition a > b is TRUE")
print(paste("Difference between a, b is : ", c))
}
# FALSE condition
if(a < b)
{
c <- a - b
print("condition a < b is TRUE")
print(paste("Difference between a, b is : ", c))
}
r
# R if-else statement Example
a <- 67
b <- 76
# This example will execute else block
if(a > b)
{
c <- a - b
print("condition a > b is TRUE")
print(paste("Difference between a, b is : ", c))
} else
{
c <- a - b
print("condition a > b is FALSE")
print(paste("Difference between a, b is : ", c))
}
r
# R if-else-if ladder Example
a <- 67
b <- 76
c <- 99
if(a > b && b > c)
{
print("condition a > b > c is TRUE")
} else if(a < b && b > c)
{
print("condition a < b > c is TRUE")
} else if(a < b && b < c)
{
print("condition a < b < c is TRUE")
}
r
# R Nested if else statement Example
a <- 10
b <- 11
if(a == 10)
{
if(b == 10)
{
print("a:10 b:10")
} else
{
print("a:10 b:11")
}
} else
{
if(a == 11)
{
print("a:11 b:10")
} else
{
print("a:11 b:11")
}
}
r
# R switch statement example
# Expression in terms of the index value
x <- switch(
2, # Expression
"Geeks1", # case 1
"for", # case 2
"Geeks2" # case 3
)
print(x)
# Expression in terms of the string value
y <- switch(
"GfG3", # Expression
"GfG0"="Geeks1", # case 1
"GfG1"="for", # case 2
"GfG3"="Geeks2" # case 3
)
print(y)
z <- switch(
"GfG", # Expression
"GfG0"="Geeks1", # case 1
"GfG1"="for", # case 2
"GfG3"="Geeks2" # case 3
)
print(z)
print(z)
输出:
[1] "condition a > b is TRUE"
[1] "Difference between a, b is : 9"
if-else 语句
if-else为我们提供了一个可选的 else 块,如果 if 块的条件为假,它就会被执行。如果提供给 if 块的条件为真,则执行 if 块中的语句,否则执行 else 块中的语句。
Syntax:
if(condition is true) {
execute this statement
} else {
execute this statement
}
流程图:
例子 :
r
# R if-else statement Example
a <- 67
b <- 76
# This example will execute else block
if(a > b)
{
c <- a - b
print("condition a > b is TRUE")
print(paste("Difference between a, b is : ", c))
} else
{
c <- a - b
print("condition a > b is FALSE")
print(paste("Difference between a, b is : ", c))
}
输出:
[1] "condition a > b is FALSE"
[1] "Difference between a, b is : -9"
if-else-if 阶梯
它类似于 if-else 语句,这里唯一的区别是 if 语句附加到 else。如果提供给 if 块的条件为真,则执行 if 块中的语句,否则 - 如果检查提供的另一个条件,如果为真,则执行块中的语句。
Syntax:
if(condition 1 is true) {
execute this statement
} else if(condition 2 is true) {
execute this statement
} else {
execute this statement
}
流程图:
例子 :
r
# R if-else-if ladder Example
a <- 67
b <- 76
c <- 99
if(a > b && b > c)
{
print("condition a > b > c is TRUE")
} else if(a < b && b > c)
{
print("condition a < b > c is TRUE")
} else if(a < b && b < c)
{
print("condition a < b < c is TRUE")
}
输出:
[1] "condition a < b < c is TRUE"
嵌套 if-else 语句
当我们将 if-else 块作为 if 块中的语句或可选地在 else 块中时,它被称为嵌套 if else 语句。当 if 条件为真时,随后的子 if 条件被验证,如果条件错误,则执行 else 语句,这发生在父 if 条件中。如果父 if 条件为假,则执行 else 块,也可能包含子 if else 语句。
Syntax:
if(parent condition is true) {
if( child condition 1 is true) {
execute this statement
} else {
execute this statement
}
} else {
if(child condition 2 is true) {
execute this statement
} else {
execute this statement
}
}
流程图:
例子:
r
# R Nested if else statement Example
a <- 10
b <- 11
if(a == 10)
{
if(b == 10)
{
print("a:10 b:10")
} else
{
print("a:10 b:11")
}
} else
{
if(a == 11)
{
print("a:11 b:10")
} else
{
print("a:11 b:11")
}
}
输出:
[1] "a:10 b:11"
切换语句
在此 switch函数中,表达式与案例列表匹配。如果找到匹配项,则打印该案例的值。此处没有可用的默认情况。如果没有匹配大小写,则输出 NULL,如示例所示。
Syntax:
switch (expression, case1, case2, case3,…,case n )
流程图:
例子:
r
# R switch statement example
# Expression in terms of the index value
x <- switch(
2, # Expression
"Geeks1", # case 1
"for", # case 2
"Geeks2" # case 3
)
print(x)
# Expression in terms of the string value
y <- switch(
"GfG3", # Expression
"GfG0"="Geeks1", # case 1
"GfG1"="for", # case 2
"GfG3"="Geeks2" # case 3
)
print(y)
z <- switch(
"GfG", # Expression
"GfG0"="Geeks1", # case 1
"GfG1"="for", # case 2
"GfG3"="Geeks2" # case 3
)
print(z)
print(z)
输出:
[1] "for"
[1] "Geeks2"
NULL