2024-05-16

LOOPING

  • WHILE LOOP
  • FOR LOOP

SAMPLE WHILE LOOP CODE

x=1
while (x<=10) {
  print(x)
  x=x+1
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10

THE CODE ABOVE LOOPS THROUGH AND OUTPUTS NUMBERS 1 TO 10

SAMPLE FOR LOOP CODE

myvec = c(1:10)
for (i in myvec) {
  print(myvec[i])
  
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
states <- c('Ogun', 'Ekiti', 'Lagos', 'Imo', 'Edo')

for (state in states) {
  print(state)
  if(state == 'Lagos')
    break # This is to stop code execution 
}
## [1] "Ogun"
## [1] "Ekiti"
## [1] "Lagos"
myvec = c(1:10)
for (i in myvec) {
  print(myvec[i]^2)
  
}
## [1] 1
## [1] 4
## [1] 9
## [1] 16
## [1] 25
## [1] 36
## [1] 49
## [1] 64
## [1] 81
## [1] 100

CONDITIONAL IF STATEMENTS

num1 <- 45
num2 <- 120

if(num1 > num2){
  print("The First Number is greater than the second")
}else{
  print("The Second Number is greater than the First")
}
## [1] "The Second Number is greater than the First"

NESTED IF STATEMENTS

ave = 100
if(ave>=70 & ave<=100){
  print("Excellent")
}else if(ave>=60 & ave<70){
  print("Very Good")
}else if(ave>=50 & ave<60){
  print("Good")
}else if(ave<50){
  print("Fail")
}else{
  print("Out of Range")
}
## [1] "Excellent"

NESTED LOOPS

lastnames = c('Olushola', 'Aluwe', 'Ojoge', 'Jude', 'Otho')
firstname = c('Lasun', 'Ujunwa', 'Shawa', 'Yusuf', 'Abdul')

for (x1 in lastnames) {
  for (x2 in firstname) {
    print(paste(x1,x2))
  }
}
## [1] "Olushola Lasun"
## [1] "Olushola Ujunwa"
## [1] "Olushola Shawa"
## [1] "Olushola Yusuf"
## [1] "Olushola Abdul"
## [1] "Aluwe Lasun"
## [1] "Aluwe Ujunwa"
## [1] "Aluwe Shawa"
## [1] "Aluwe Yusuf"
## [1] "Aluwe Abdul"
## [1] "Ojoge Lasun"
## [1] "Ojoge Ujunwa"
## [1] "Ojoge Shawa"
## [1] "Ojoge Yusuf"
## [1] "Ojoge Abdul"
## [1] "Jude Lasun"
## [1] "Jude Ujunwa"
## [1] "Jude Shawa"
## [1] "Jude Yusuf"
## [1] "Jude Abdul"
## [1] "Otho Lasun"
## [1] "Otho Ujunwa"
## [1] "Otho Shawa"
## [1] "Otho Yusuf"
## [1] "Otho Abdul"
lastnames = c('Olushola', 'Aluwe', 'Ojoge', 'Jude', 'Otho')
firstname = c('Lasun', 'Ujunwa', 'Shawa', 'Yusuf', 'Abdul')

for (x1 in lastnames) {
  for (x2 in firstname) {
   
  }
   print(paste(x1,x2))
}
## [1] "Olushola Abdul"
## [1] "Aluwe Abdul"
## [1] "Ojoge Abdul"
## [1] "Jude Abdul"
## [1] "Otho Abdul"
lastnames = c('Olushola', 'Aluwe', 'Ojoge', 'Jude', 'Otho')
firstname = c('Lasun', 'Ujunwa', 'Shawa', 'Yusuf', 'Abdul')

for (x1 in lastnames) {
     print(paste(x1))
}
## [1] "Olushola"
## [1] "Aluwe"
## [1] "Ojoge"
## [1] "Jude"
## [1] "Otho"
for (x2 in firstname) {
   print(paste(x2))
  }
## [1] "Lasun"
## [1] "Ujunwa"
## [1] "Shawa"
## [1] "Yusuf"
## [1] "Abdul"

FUNCTIONS

functions are re-usable codes that accept parameters and return values with the use of return statement.

funcgreetme = function()
  print("Good Morning")

funcgreetme()
## [1] "Good Morning"

functions with arguments

square <- function(x){
  return(x^2)
}

square(500)
## [1] 250000
for (name in (names<-c(1:12))) {
 # print(name)
  
  if(name==5){
    sqr = name^2
    print(sqr)
  }else{
    sqr2 = name ^4
    print(sqr2)
  }
  
}
## [1] 1
## [1] 16
## [1] 81
## [1] 256
## [1] 25
## [1] 1296
## [1] 2401
## [1] 4096
## [1] 6561
## [1] 10000
## [1] 14641
## [1] 20736