R语言11-R语言中的条件结构

if语句: if 语句用于在条件为真时执行一段代码块。

python 复制代码
x <- 10
if (x > 5) {
  print("x is greater than 5")
}

if-else语句: if-else 语句允许您在条件为真和条件为假时执行不同的代码块。

python 复制代码
x <- 3
if (x > 5) {
  print("x is greater than 5")
} else {
  print("x is not greater than 5")
}

if-else if-else语句: 可以在 if 和 else 之间添加多个 else if 分支,以处理多个条件情况。

python 复制代码
x <- 7
if (x > 10) {
  print("x is greater than 10")
} else if (x > 5) {
  print("x is greater than 5 but not greater than 10")
} else {
  print("x is not greater than 5")
}

switch语句: switch 语句用于根据某个表达式的值执行不同的代码块。

python 复制代码
day <- "Monday"
switch(day,
  "Monday" = print("It's the start of the week"),
  "Friday" = print("It's the end of the week"),
  print("It's another day")
)

逻辑运算符: 逻辑运算符如 &&(与)、||(或)和 !(非)用于在条件中组合多个条件

python 复制代码
x <- 8
if (x > 5 && x < 10) {
  print("x is between 5 and 10")
}
相关推荐
逆风优雅17 分钟前
python 爬取网站图片的小demo
开发语言·python
m0_6161884933 分钟前
PDF预览-搜索并高亮文本
开发语言·javascript·ecmascript
IT瘾君34 分钟前
Java基础:Logback日志框架
java·开发语言·logback
stevenzqzq37 分钟前
kotlin中主构造函数是什么
开发语言·python·kotlin
Tttian6221 小时前
Python办公自动化(2)对word&pdf的操作
开发语言·python
美美打不死1 小时前
webpack js 逆向 --- 个人记录
开发语言·javascript·webpack
等雨季1 小时前
scala编程语言
开发语言·scala
不断前进的皮卡丘1 小时前
06-公寓租赁项目-后台管理-公寓管理篇
java·开发语言·数据库·spring boot
努力学习的小廉1 小时前
【C++11(下)】—— 我与C++的不解之缘(三十二)
开发语言·c++
唐静蕴2 小时前
Kotlin语言的安全开发
开发语言·后端·golang