Members

1.Eugene Otolo 2. Collins Bosire
3. Kabindio Mathews 4. Jairus Otana
5. Vincent Otsieno 6. Norbert Nyagah 7. Nicholas Kipkemboi 8. Daniel Nzioki

1. Introduction

Flow control statements are used in to control the sequence of execution of code. These statements help to implement decision-making, looping, and branching logic, enabling the development of dynamic and responsive programs. ## Create a sample data frame

student_data <- data.frame(
  name = c("Alice", "Bob", "Charlie", "Diana", "Ethan"),
  score = c(85, 42, 73, 90, 55)
)

print(student_data)
##      name score
## 1   Alice    85
## 2     Bob    42
## 3 Charlie    73
## 4   Diana    90
## 5   Ethan    55

a) Conditional Statements

if Statement

if (student_data$score[1] > 80) {
  print(paste(student_data$name[1], "passed with distinction"))
}
## [1] "Alice passed with distinction"

if…else Statement

Check if Bob passed or failed (pass mark = 50)

if (student_data$score[2] >= 50) {
  print(paste(student_data$name[2], "passed"))
} else {
  print(paste(student_data$name[2], "failed"))
}
## [1] "Bob failed"

if…else if…else

Check Charlie’s grade category

score <- student_data$score[3]

if (score >= 85) {
  print("Grade: A")
} else if (score >= 70) {
  print("Grade: B")
} else if (score >= 50) {
  print("Grade: C")
} else {
  print("Grade: D")
}
## [1] "Grade: B"

##Looping Statements * Loops are used to repeat a block of code multiple times.

for Loop

*Used when the number of iterations is known.

Print each student’s name and whether they passed

for (i in 1:nrow(student_data)) {
  if (student_data$score[i] >= 50) {
    print(paste(student_data$name[i], "passed"))
  } else {
    print(paste(student_data$name[i], "failed"))
  }
}
## [1] "Alice passed"
## [1] "Bob failed"
## [1] "Charlie passed"
## [1] "Diana passed"
## [1] "Ethan passed"

while Loop

Print scores until a student scores less than 60

i <- 1
while (i <= nrow(student_data) && student_data$score[i] >= 60) {
  print(paste(student_data$name[i], "has", student_data$score[i], "marks"))
  i <- i + 1
}
## [1] "Alice has 85 marks"

repeat Loop

Repeat until we find a student with a score below 50

i <- 1
repeat {
  if (student_data$score[i] < 50) {
    print(paste(student_data$name[i], "has failed with", student_data$score[i], "marks"))
    break
  }
  i <- i + 1
}
## [1] "Bob has failed with 42 marks"

Control Flow within Loops

*break: Exits the loop early.

break Statement in Loop

Break the loop once you find a student with distinction (score > 85)

for (i in 1:nrow(student_data)) {
  if (student_data$score[i] > 85) {
    print(paste(student_data$name[i], "has distinction!"))
    break
  }
}
## [1] "Diana has distinction!"

next Statement

*next: Skips the current iteration and moves to the next. Skip printing students who scored below 60

for (i in 1:nrow(student_data)) {
  if (student_data$score[i] < 60) {
    next
  }
  print(paste(student_data$name[i], "score:", student_data$score[i]))
}
## [1] "Alice score: 85"
## [1] "Charlie score: 73"
## [1] "Diana score: 90"

return() Function

Function to get pass/fail message for a student

check_pass <- function(score) {
  if (score >= 50) {
    return("Pass")
  } else {
    return("Fail")
  }
}

check_pass(student_data$score[5])  # For Ethan
## [1] "Pass"

Nested Loops

Compare each student’s score with every other student

for (i in 1:nrow(student_data)) {
  for (j in 1:nrow(student_data)) {
    if (i != j) {
      print(paste(student_data$score[i], "vs", student_data$score[j]))
    }
  }
}
## [1] "85 vs 42"
## [1] "85 vs 73"
## [1] "85 vs 90"
## [1] "85 vs 55"
## [1] "42 vs 85"
## [1] "42 vs 73"
## [1] "42 vs 90"
## [1] "42 vs 55"
## [1] "73 vs 85"
## [1] "73 vs 42"
## [1] "73 vs 90"
## [1] "73 vs 55"
## [1] "90 vs 85"
## [1] "90 vs 42"
## [1] "90 vs 73"
## [1] "90 vs 55"
## [1] "55 vs 85"
## [1] "55 vs 42"
## [1] "55 vs 73"
## [1] "55 vs 90"

Conclusion

Flow control statements are essential for writing effective, dynamic, and flexible code. Understanding how to use them correctly allows us to perform complex data manipulations, statistical computations, and automation tasks with precision.