Skip to content

Latest commit

 

History

History
61 lines (59 loc) · 759 Bytes

2_loops.md

File metadata and controls

61 lines (59 loc) · 759 Bytes

Loops Repeat Actions

if else if

a<-6
if(a>5){
  print("a is greater than 5")
}else if (a==5){
  print("a is equal to 5 ")
}else{
  print("a is less than 5")
}

read user input

# greater-than >
# less-than <
age<-as.integer(readline(prompt = "Enter the age: "))
if(age<=10){
  print("kids")
}else{
  print("adult")
}

switch statement

vtr<-c(1,2,3,4,5)
option<-readline(prompt = "Enter the option: ")
switch (option,
        "mean"=print(mean(vtr)),
        "median"=print(median(vtr)),
        "min"=print(min(vtr))
)

loops repeat

x=2
repeat{
  x=x^2
  print(x)
  if(x>15)
    break
}

While

while(x<100){
  x=x^2
  print(x)
}

for

for (i in 1:15) {
  if((i%%2)==0){
    next
  }
  print(i)
}