Conditions, boucles
If … Else
Le mieux est d’illustrer ceci sur un exemple :
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")
} Nested If
Sur un exemple :
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.")
} AND, OR
Un exemple pour AND
a <- 200
b <- 33
c <- 500
if (a > b & c > a) {
print("Both conditions are true")
} Un exemple pour OR
a <- 200
b <- 33
c <- 500
if (a > b | a > c) {
print("At least one of the conditions is true")
}Loops
R a deux commandes pour les boucles : - while loops - for loops
i <- 1
while (i < 6) {
print(i)
i <- i + 1
}un autre exemple
i <- 1
while (i < 6) {
print(i)
i <- i + 1
if (i == 4) {
break
}
} Un dernier exemple pour illsutrer la fonction next
i <- 0
while (i < 6) {
i <- i + 1
if (i == 3) {
next
}
print(i)
} Un exemple pour une boucle for
fruits <- list("apple", "banana", "cherry")
for (x in fruits) {
print(x)
} ou encore
adj <- list("red", "big", "tasty")
fruits <- list("apple", "banana", "cherry")
for (x in adj) {
for (y in fruits) {
print(paste(x, y))
}
}