Petlje
Zadatak: Predstaviti primjere for
, repeat
i while
petlje.
> #for petlja
> for(i in 1:3) {
+ print(i)
+ }
[1] 1
[1] 2
[1] 3
> #while petlja
> x <- 1
> while (x < 3) {
+ print(x)
+ x <- x + 1
+ }
[1] 1
[1] 2
> #repeat loop
> x <- 5
> repeat{
+ print(x)
+ x <- x + 1
+ if(x > 8) {break}
+ }
[1] 5
[1] 6
[1] 7
[1] 8
Last updated
Was this helpful?