If Loop

An example of a for loop syntax:

x <- 3
y <- 2
if (x<=y) {
  print("x smaller than y")
  } else {
  print("x larger than y")
}
## [1] "x larger than y"

Example of a for loop

j <- 0
for (i in 1:3){
  j <- i+j}
print(j)
## [1] 6

While loop example:

x <- 2
y <- 1
while(x+y<6){
  x<-x+y
  print(x+y)}
## [1] 4
## [1] 5
## [1] 6

Example of function: Can have default values if we specify them in the function() statement

CDF.pois <- function(x, lambda){
  cdf = 0
  for (k in 0:x){
    cdf = cdf + exp(-lambda)*lambda^k/factorial(k)
  }
  return(cdf)
}