R语言 决策–if、if 您所在的位置:网站首页 r的if语句 R语言 决策–if、if

R语言 决策–if、if

2024-07-11 19:17| 来源: 网络整理| 查看: 265

R语言 决策–if、if-else、if-else-if梯子、嵌套if-else和switch

决策是关于根据某些条件来决定语句的执行顺序。在决策中,程序员需要提供一些由程序评估的条件,同时还提供一些如果条件为真则执行的语句,如果条件被评估为假则可选其他语句。

R语言中的决策语句如下。

if 语句 if-else 语句 if-else-if 梯子 嵌套的if-else语句 switch语句 if语句

关键字 if 告诉编译器这是一条决策控制指令,关键字if后面的条件总是包含在一对小括号中。如果条件为 “true”,语句就会被执行;如果条件为 “false”,语句就不会被执行。

语法

if(condition is true){

执行该语句

}

流程图

R编程中的决策--if、if-else、if-else-if梯子、嵌套if-else和switch

例子

# R program to illustrate # if statement a 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语句。

语法

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编程中的决策--if、if-else、if-else-if梯子、嵌套if-else和switch

例子

# R Nested if else statement Example a


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有