# Mindanao State University
# General Santos City
# Mathematics Department
# March 30, 2021

# Basic Programming in R
# Submitted by: Joshua Marie H. Casador
# Prepared by: Prof. Carlito Daarol
# for BS Math/BS IT/BS Econ students
# during the second pandemic semester

# Triple for loops

# This is the way how to establish and update a counter
# within nested loops
count = 0
for (i in 1:5){
  for (j in 1:4){
    for (k in 1:3){
      count = count +1
    }
  }
}
print(count)
## [1] 60
#Enumerate all values of the index as implied by nested loops
#Using paste0 command
count = 0
for (i in 1:4){
  for (j in 1:3){
    for (k in 1:2){
      1
      count = count +1
      print(noquote(paste0(count, " : i= ",i, " | j= ", j , " | k= ", k )))
    }
  }
}
## [1] 1 : i= 1 | j= 1 | k= 1
## [1] 2 : i= 1 | j= 1 | k= 2
## [1] 3 : i= 1 | j= 2 | k= 1
## [1] 4 : i= 1 | j= 2 | k= 2
## [1] 5 : i= 1 | j= 3 | k= 1
## [1] 6 : i= 1 | j= 3 | k= 2
## [1] 7 : i= 2 | j= 1 | k= 1
## [1] 8 : i= 2 | j= 1 | k= 2
## [1] 9 : i= 2 | j= 2 | k= 1
## [1] 10 : i= 2 | j= 2 | k= 2
## [1] 11 : i= 2 | j= 3 | k= 1
## [1] 12 : i= 2 | j= 3 | k= 2
## [1] 13 : i= 3 | j= 1 | k= 1
## [1] 14 : i= 3 | j= 1 | k= 2
## [1] 15 : i= 3 | j= 2 | k= 1
## [1] 16 : i= 3 | j= 2 | k= 2
## [1] 17 : i= 3 | j= 3 | k= 1
## [1] 18 : i= 3 | j= 3 | k= 2
## [1] 19 : i= 4 | j= 1 | k= 1
## [1] 20 : i= 4 | j= 1 | k= 2
## [1] 21 : i= 4 | j= 2 | k= 1
## [1] 22 : i= 4 | j= 2 | k= 2
## [1] 23 : i= 4 | j= 3 | k= 1
## [1] 24 : i= 4 | j= 3 | k= 2
#Add text for each line
count = 0
for (i in 20:21){
  for (j in 15:16){
    for (k in 1:3){
      count = count +1
      if (count < 10) {
        print(paste0(count, " : i= ",i, " | j= ", j , " | k= ", k , " | hello babes"))
      } else {
        print(paste0(count, ": i= ",i, " | j= ", j , " | k= ", k , " | hello babes"))
      }
    }
  }
}
## [1] "1 : i= 20 | j= 15 | k= 1 | hello babes"
## [1] "2 : i= 20 | j= 15 | k= 2 | hello babes"
## [1] "3 : i= 20 | j= 15 | k= 3 | hello babes"
## [1] "4 : i= 20 | j= 16 | k= 1 | hello babes"
## [1] "5 : i= 20 | j= 16 | k= 2 | hello babes"
## [1] "6 : i= 20 | j= 16 | k= 3 | hello babes"
## [1] "7 : i= 21 | j= 15 | k= 1 | hello babes"
## [1] "8 : i= 21 | j= 15 | k= 2 | hello babes"
## [1] "9 : i= 21 | j= 15 | k= 3 | hello babes"
## [1] "10: i= 21 | j= 16 | k= 1 | hello babes"
## [1] "11: i= 21 | j= 16 | k= 2 | hello babes"
## [1] "12: i= 21 | j= 16 | k= 3 | hello babes"
paste0("The triple loop displayed " , count, " lines of hello babes. ")
## [1] "The triple loop displayed 12 lines of hello babes. "
noquote(paste0("The triple loop displayed " , count, " lines of hello babes. "))
## [1] The triple loop displayed 12 lines of hello babes.
# In your official exam, expect to answer questions like this
# What do you think why I put up two print statements inside the triple loop?
# What is the output if I will consider only one print statements?
# The answer is very short and you have to convince me briefly and exactly.
# Example 5: triple for loop with a computed value
# Suppose that Age is a function of i, and k and it is defined as
# Age = 14 + i + 3j + 5k
# How many lines will be displayed? What is the value of Age for each i,j and k
# the solution is easy, just insert the equation after the count line
count = 0
for (i in 20:21){
  for (j in 15:16){
    for (k in 1:3){
      count = count +1
      Age <- 14 + i + 3*j + 5*k
      if (count < 10) {
        print(paste0(count, " : i= ",i, " | j= ", j , " | k= ", k , " | ", "Age = ", Age))
      } else {
        print(paste0(count, ": i= ",i, " | j= ", j , " | k= ", k , " | ", "Age = ", Age))
      }
    }
  }
}
## [1] "1 : i= 20 | j= 15 | k= 1 | Age = 84"
## [1] "2 : i= 20 | j= 15 | k= 2 | Age = 89"
## [1] "3 : i= 20 | j= 15 | k= 3 | Age = 94"
## [1] "4 : i= 20 | j= 16 | k= 1 | Age = 87"
## [1] "5 : i= 20 | j= 16 | k= 2 | Age = 92"
## [1] "6 : i= 20 | j= 16 | k= 3 | Age = 97"
## [1] "7 : i= 21 | j= 15 | k= 1 | Age = 85"
## [1] "8 : i= 21 | j= 15 | k= 2 | Age = 90"
## [1] "9 : i= 21 | j= 15 | k= 3 | Age = 95"
## [1] "10: i= 21 | j= 16 | k= 1 | Age = 88"
## [1] "11: i= 21 | j= 16 | k= 2 | Age = 93"
## [1] "12: i= 21 | j= 16 | k= 3 | Age = 98"
noquote(paste0("The triple loop displayed " , count, " lines of Age for each i, j and k"))
## [1] The triple loop displayed 12 lines of Age for each i, j and k
# Next exercise will illustrate how to
# consolidate a sequence of single loops into 1 double loop
# consolidate a sequence of double loops into 1 triple