Contoh Sintaks Looping

#for statement

#1 Program : membangkitkan bilangan Fibonacci untuk 'n' bilangan n <- 10

n <- 10

fib <- numeric(n)

fib[1] <- 1

fib[2] <- 1

for (i in 3:n)

(

fib[i] <- fib[i-1]+fib[i-2]

)

fib


#while statement

#2 Program : membangkitkan bilagan Fibonacci hingga 'n' bilangan

n <- 10

fib1 <- 1

fib2 <- 1

fib <- fib1

while (fib2 < n)

{

fib <- c(fib, fib2)

Temp <- fib2

fib2 <- fib1 + fib2

fib1 <- Temp

}

fib

 

#repeat statement

#3 Program : print sequence of 'n'  numbers

n <- 0

repeat

{

n <- n + 1

if(n > 10)

          {

          break

          }

print(n)

}




Comments

Popular Posts