128
Le logiciel R - Maˆ ıtriser le langage
Astuce
La liste d’instructions ci-dessous fait d´ efiler un compteur d´ ecroissant :
n <- 100 ; for (i in 1:n) {flush.console();cat(n-i,"\r");
Sys.sleep(0.1)}
• Instruction while() (tant que)
La syntaxe de cette instruction est la suivante :
while()
Par exemple :
> x <- 2
> y <- 1
> while(x+y<7) x <- x+y
> x
[1] 6
• Instructions next (suivant), break (interrompre)
> for (i in 1:4) {
+
if (i == 3) break
+
for (j in 6:8) {
+
if (j==7) next
+
j <- i+j
+
}
+ }
> i
[1] 3
> j
[1] 10
• Instructions repeat (r´ ep´ eter), break (interrompre)
> i <- 0
> repeat {
+
i<-i+1
+
if (i==4) break
+ }
Le logiciel R - Maˆ ıtriser le langage
Astuce
La liste d’instructions ci-dessous fait d´ efiler un compteur d´ ecroissant :
n <- 100 ; for (i in 1:n) {flush.console();cat(n-i,"\r");
Sys.sleep(0.1)}
• Instruction while() (tant que)
La syntaxe de cette instruction est la suivante :
while(
Par exemple :
> x <- 2
> y <- 1
> while(x+y<7) x <- x+y
> x
[1] 6
• Instructions next (suivant), break (interrompre)
> for (i in 1:4) {
+
if (i == 3) break
+
for (j in 6:8) {
+
if (j==7) next
+
j <- i+j
+
}
+ }
> i
[1] 3
> j
[1] 10
• Instructions repeat (r´ ep´ eter), break (interrompre)
> i <- 0
> repeat {
+
i<-i+1
+
if (i==4) break
+ }
