Bedingte Anweisungen

R für Fortgeschrittene

Filip Schouwenaars

DataCamp Instructor

if-Anweisung

if(condition) {
  expr
}
x <- -3
if(x < 0) {
  print("x is a negative number")
}
"x is a negative number"
R für Fortgeschrittene

if-Anweisung

if(condition) {
  expr
}
x <- 5
if(x < 0) {
  print("x is a negative number")
}
  • Kein Ausdruck
R für Fortgeschrittene

else-Anweisung

if(condition) {
  expr1
} else {
  expr2
}
x <- -3
if(x < 0) {
  print("x is a negative number")
} else {
  print("x is either a positive number or zero")
}
"x is a negative number"
R für Fortgeschrittene

else-Anweisung

if(condition) {
  expr1
} else {
  expr2
}
x <- 5
if(x < 0) {
  print("x is a negative number")
} else {
  print("x is either a positive number or zero")
}
"x is either a positive number or zero"
R für Fortgeschrittene

sonst wenn-Anweisung

if(condition1) {
  expr1
} else if(condition2) {
  expr2
} else {
  expr3
}
R für Fortgeschrittene

sonst wenn-Anweisung

x <- -3
if(x < 0) {
  print("x is a negative number")
} else if(x == 0) {
  print("x is zero")
} else {
  print("x is a positive number")
}
"x is a negative number"
R für Fortgeschrittene

sonst wenn-Anweisung

x <- 0
if(x < 0) {
  print("x is a negative number")
} else if(x == 0) {
  print("x is zero")
} else {
  print("x is a positive number")
}
"x is zero"
R für Fortgeschrittene

sonst wenn-Anweisung

x <- 5
if(x < 0) {
  print("x is a negative number")
} else if(x == 0) {
  print("x is zero")
} else {
  print("x is a positive number")
} 
"x is a positive number"
R für Fortgeschrittene

wenn, sonst wenn, sonst

x <- 6
if(x %% 2 == 0) {
  print("divisible by 2")
} else if(x %% 3 == 0) {
  print("divisible by 3")
} else {
  print("not divisible by 2 nor by 3...")
}
"divisible by 2"
R für Fortgeschrittene

Lass uns üben!

R für Fortgeschrittene

Preparing Video For Download...