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