فصل 10 دستورات شرطی

دستورات شرطی برای تصمیم‌گیری و جهت دهی جریان پردازش اطلاعات استفاده می‌شوند.

  • if
Code
a <- 33
b <- 200

if (b > a) {
  print("b is greater than a")
}
[1] "b is greater than a"
  • if else
Code
a <- 200
b <- 33

if (b > a) {
  print("b is greater than a")
} else {
  print("b is not greater than a")
}
"b is not greater than a"
  • if else if
Code
a <- 33
b <- 33

if (b > a) {
  print("b is greater than a")
} else if (a == b) {
  print ("a and b are equal")
}
"a and b are equal"
  • if else if else
Code
a <- 200
b <- 33

if (b > a) {
  print("b is greater than a")
} else if (a == b) {
  print("a and b are equal")
} else {
  print("a is greater than b")
}
[1] "a is greater than b"
  • شروط تو در تو
Code
x <- 41

if (x > 10) {
  print("Above ten")
  if (x > 20) {
    print("and also above 20!")
  } else {
    print("but not above 20.")
  }
} else {
  print("below 10.")
}
[1] "Above ten"
[1] "and also above 20!"
  • و
Code
a <- 200
b <- 33
c <- 500

if (a > b & c > a) {
  print("Both conditions are true")
}
[1] "Both conditions are true"
  • یا
Code
a <- 200
b <- 33
c <- 500

if (a > b | a > c) {
  print("At least one of the conditions is true")
}
[1] "At least one of the conditions is true"
  • ifelse()
Code
a <- 3
b <- 5

ifelse((a < b), "b is greater than a", "b is not greater than a")
[1] "b is greater than a"