Practicum Week 4—Syntax and Control Flow

Rafael Yogi Septiadi Putra

RAFAEL YOGI SEPTIADI P.

Data Science Major

Data Science
Major Data Science
Data Science
Student ID 52250019
Data Science
Lecturer Bakti Siregar, M. Sc., CSD.
Subject Data Science Programming I

Introduction

In data science workflows, automating decision-making processes and iterating through large datasets are foundational skills. This report demonstrates the application of conditional statements (if-else) and looping mechanisms (for, while) to manage employee data.

The primary objectives include:

  • Calculating performance-based bonuses.
  • Filtering records based on financial thresholds.
  • Controlling execution flow using break and next statements.

Initializing the Employee Dataset

We begin by defining a structured dummy dataset representing an organization’s human resources.

Table 1: Table 1: Raw Employee Records
ID Name Age Salary Position Performance
1 Bagas 25 5000 Staff Good
2 Joan 30 7000 Supervisor Very Good
3 Alya 27 6500 Staff Average
4 Dwi 35 10000 Manager Good
5 Nabil 40 12000 Director Very Good

Implementation

Conditional Logic: Bonus Calculation

In this section, we apply a tiered bonus structure based on the Performance metric:

  • Very Good: 20% of Salary

  • Good: 10% of Salary

  • Average: 5% of Salary

cat("--- Individual Bonus Report ---\n")
## --- Individual Bonus Report ---
for (i in 1:nrow(employees)) {
  salary <- employees$Salary[i]
  perf <- employees$Performance[i]
  
  # Conditional Logic
  if (perf == "Very Good") {
    bonus <- 0.20 * salary
  } else if (perf == "Good") {
    bonus <- 0.10 * salary
  } else if (perf == "Average") {
    bonus <- 0.05 * salary
  } else {
    bonus <- 0
  }
  
  cat(sprintf("Employee: %-8s | Bonus: %-5.0f\n", employees$Name[i], bonus))
}
## Employee: Bagas    | Bonus: 500  
## Employee: Joan     | Bonus: 1400 
## Employee: Alya     | Bonus: 325  
## Employee: Dwi      | Bonus: 1000 
## Employee: Nabil    | Bonus: 2400

Iterative Filtering (For Loop)

Looping Concept: For Loop A for loop is used to iterate over a sequence (in this case, the rows of the dataframe). It is ideal when the number of iterations is known beforehand.

cat("High-Income Filter (Salary > 6,000):\n")
## High-Income Filter (Salary > 6,000):
for (i in 1:nrow(employees)) {
  if (employees$Salary[i] > 6000) {
    cat(paste("  -", employees$Name[i], "| Salary:", employees$Salary[i], "\n"))
  }
}
##   - Joan | Salary: 7000 
##   - Alya | Salary: 6500 
##   - Dwi | Salary: 10000 
##   - Nabil | Salary: 12000

Conditional Interruption (While Loop & Break)

Control Flow: While & Break A while loop continues as long as a condition is true. The break statement is used to exit the loop immediately when a specific target (e.g., Position == “Manager”) is met.

cat("Manager Search Log:\n")
## Manager Search Log:
i <- 1
while (i <= nrow(employees)) {
  cat(paste("Checking ID", i, ":", employees$Name[i], "[", employees$Position[i], "]"))
  
  if (employees$Position[i] == "Manager") {
    cat(" -> [MATCH FOUND: STOPPING]\n")
    break
  }
  cat("\n")
  i <- i + 1
}
## Checking ID 1 : Bagas [ Staff ]
## Checking ID 2 : Joan [ Supervisor ]
## Checking ID 3 : Alya [ Staff ]
## Checking ID 4 : Dwi [ Manager ] -> [MATCH FOUND: STOPPING]

Exception Handling (Next/Continue)

Control Flow: Next

The next statement in R (equivalent to continue in Python) skips the current iteration and jumps to the next one. This is useful for filtering out noise or specific categories without stopping the entire process.

cat("Performance Quality Audit (Excluding 'Average'):\n")
## Performance Quality Audit (Excluding 'Average'):
for (i in 1:nrow(employees)) {
  if (employees$Performance[i] == "Average") {
    next # Skips the current iteration
  }
  cat(paste("  [OK]", employees$Name[i], "| Performance:", employees$Performance[i], "\n"))
}
##   [OK] Bagas | Performance: Good 
##   [OK] Joan | Performance: Very Good 
##   [OK] Dwi | Performance: Good 
##   [OK] Nabil | Performance: Very Good

Conclusion

The implementation successfully demonstrates the utility of control structures in R. By leveraging if-elif-else logic, we automated compensation adjustments, while loops and control statements provided granular control over dataset traversal. These techniques are essential for building scalable data pipelines and automated reporting systems in professional data science environments.

Reference

Dsciencelabs. (2024). Syntax and Control Flow. Data Science Programming. Source Link