Foto

Fityanandra Athar Adyaksa (52250059)


Data Science students at

Enthusiastic about learning

March 15, 2026



Practicum

This practicum covers Section 2.5 of Data Science Programming – Syntax and Control Flow. The objectives are:

  1. Implement conditional statements (if, if-else, if-elif-else) to determine employee bonuses.
  2. Apply loops (for, while, break, next) to analyze a simple employee dataset.

Dataset

The following dummy dataset represents five employees with attributes including name, age, salary, position, and performance rating. This dataset serves as the foundation for all practicum tasks in this section.

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



2.5.2 Conditional Statements

– Bonus Calculation

Conditional statements allow a program to execute different code blocks based on whether a given condition is true or false. In this task, an if-else structure evaluates each employee’s performance rating and assigns a bonus percentage accordingly.

The logic applied in both R and Python follows the same three-branch structure:

While the logic is identical, there are minor syntactic differences between the two languages worth noting:

Language R Python
Branch keyword else if elif
Iteration seq_len(nrow()) + index i direct loop over list
String formatting paste0() f-string
Number formatting automatic int() to remove .0


  • R code

# Iterate over each row in the employees data frame
for (i in seq_len(nrow(employees))) {
  
  # Extract employee attributes for current iteration
  name        <- employees$name[i]        # Get employee name
  salary      <- employees$salary[i]      # Get employee salary
  performance <- employees$performance[i] # Get performance rating

  # --- Determine bonus percentage based on performance rating ---
  if (performance == "Very Good") {
    bonus <- salary * 0.20  # Very Good → 20% of salary
  } else if (performance == "Good") {
    bonus <- salary * 0.10  # Good → 10% of salary
  } else {
    bonus <- salary * 0.05  # Average → 5% of salary
  }

  # Print result in the required format
  print(paste0("Name: ", name, ", Bonus: ", bonus))
}
## [1] "Name: Bagas, Bonus: 500"
## [1] "Name: Joan, Bonus: 1400"
## [1] "Name: Alya, Bonus: 325"
## [1] "Name: Dwi, Bonus: 1000"
## [1] "Name: Nabil, Bonus: 2400"


  • Python Code

# Iterate directly over the list of dictionaries
for emp in employees:

    # Extract employee attributes for current iteration
    name        = emp["name"]        # Get employee name
    salary      = emp["salary"]      # Get employee salary
    performance = emp["performance"] # Get performance rating

    # --- Determine bonus percentage based on performance rating ---
    if performance == "Very Good":
        bonus = salary * 0.20   # Very Good → 20% of salary
    elif performance == "Good":
        bonus = salary * 0.10   # Good → 10% of salary
    else:
        bonus = salary * 0.05   # Average → 5% of salary

    # Print result — int() removes decimal notation (e.g. 500.0 → 500)
    print(f"Name: {name}, Bonus: {int(bonus)}")
## Name: Bagas, Bonus: 500
## Name: Joan, Bonus: 1400
## Name: Alya, Bonus: 325
## Name: Dwi, Bonus: 1000
## Name: Nabil, Bonus: 2400



2.5.3 Loops (For & While)

Task 1 – For Loop

A for loop iterates over a sequence of elements for a predetermined number of iterations. In this task, the loop traverses the entire dataset and applies a numeric threshold condition to filter employees whose salary exceeds 6,000.

The filtering logic works as follows:

  • Each employee record is evaluated against the condition salary > 6000
  • Records that satisfy the condition are printed with their name and salary
  • Bagas, whose salary is 5,000, does not meet the threshold and is therefore excluded from the output

The two implementations share the same logic but differ in syntax:

Language R Python
Iteration seq_len(nrow()) + index i direct loop over list
Data access employees$Salary[i] emp["salary"]
String formatting paste0() f-string


  • R code

# Iterate over each row in the employees data frame
for (i in seq_len(nrow(employees))) {

  # --- Check if current employee's salary exceeds the threshold ---
  if (employees$salary[i] > 6000) {

    # Print name and salary if condition is met
    print(paste0("Name: ", employees$Name[i], ", Salary: ", employees$salary[i]))
  }
  # If salary <= 6000, no action is taken — iteration moves to next row
}
## [1] "Name: , Salary: 7000"
## [1] "Name: , Salary: 6500"
## [1] "Name: , Salary: 10000"
## [1] "Name: , Salary: 12000"


  • Python code

# Iterate directly over the list of dictionaries
for emp in employees:

    # --- Check if current employee's salary exceeds the threshold ---
    if emp["salary"] > 6000:

        # Print name and salary if condition is met
        # If salary <= 6000, no action is taken — iteration moves to next item
        print(f"Name: {emp['name']}, Salary: {emp['salary']}") 
## Name: Joan, Salary: 7000
## Name: Alya, Salary: 6500
## Name: Dwi, Salary: 10000
## Name: Nabil, Salary: 12000


Task 2 – While Loop

A while loop continues execution as long as its condition remains true, making it suitable for situations where the number of iterations is not known in advance. In this task, the loop traverses the dataset sequentially and terminates as soon as an employee with the position of “Manager” is encountered.

The termination logic works as follows:

  • On each iteration, the employee’s position is evaluated
  • If the position is not “Manager”, the record is printed normally and the counter advances
  • If the position is “Manager”, the record is printed with the label (Stop here) and break immediately exits the loop
  • Nabil, who comes after Dwi, is never reached

A key difference between the two implementations:

Language R Python
Index origin i <- 1 (1-based) i = 0 (0-based)
Loop condition i <= nrow(employees) i < len(employees)
Counter increment i <- i + 1 i += 1
Data access employees$Position[i] employees[i]["position"]


  • R code

# Use a while loop to display employees until a "Manager" is found
i <- 1
while (i <= nrow(employees)) {
  name     <- employees$name[i]
  position <- employees$position[i]

  if (position == "Manager") {
    print(paste0("Name: ", name, ", Position: ", position, " (Stop here)"))
    break
  }

  print(paste0("Name: ", name, ", Position: ", position))
  i <- i + 1
}
## [1] "Name: Bagas, Position: Staff"
## [1] "Name: Joan, Position: Supervisor"
## [1] "Name: Alya, Position: Staff"
## [1] "Name: Dwi, Position: Manager (Stop here)"


  • Python code

i = 0  # Initialize counter — Python uses 0-based indexing

# Continue looping as long as counter does not exceed list length
while i < len(employees):

    # Extract employee attributes for current iteration
    name     = employees[i]["name"]     # Get employee name
    position = employees[i]["position"] # Get employee position

    # --- Check if current employee is a Manager ---
    if position == "Manager":
        # Print final record with stop label and exit the loop
        print(f"Name: {name}, Position: {position} (Stop here)")
        break  # Terminate the loop immediately

    # Print current record if not a Manager
    print(f"Name: {name}, Position: {position}")

    i += 1  # Increment counter to move to next employee
## Name: Bagas, Position: Staff
## Name: Joan, Position: Supervisor
## Name: Alya, Position: Staff
## Name: Dwi, Position: Manager (Stop here)


Task 3 – Break

The break statement provides a mechanism to exit a loop immediately when a specific condition is met, regardless of how many iterations remain. In this task, the loop scans each employee’s salary and halts as soon as one strictly exceeds 10,000.

The termination logic works as follows:

  • On each iteration, salary > 10000 is evaluated before printing
  • If the condition is false, the record is printed normally
  • If the condition is true, a termination message is printed and break exits the loop immediately
  • Dwi, whose salary is exactly 10,000, is still printed because 10000 > 10000 evaluates to false
  • Nabil, whose salary is 12,000, triggers the break condition

Both R and Python share identical logic with no structural differences in this task:

Language R Python
Loop type for + seq_len() for over list
Break condition salary > 10000 salary > 10000
Termination message paste0() f-string

  • R code

# Use break to stop the loop when salary above 10,000 is found
for (i in 1:nrow(employees)) {
  name   <- employees$name[i]
  salary <- employees$salary[i]

  if (salary > 10000) {
    print(paste0("(Stopped because ", name, " has a salary above 10,000)"))
    break
  }

  print(paste0("Name: ", name, ", Salary: ", salary))
}
## [1] "Name: Bagas, Salary: 5000"
## [1] "Name: Joan, Salary: 7000"
## [1] "Name: Alya, Salary: 6500"
## [1] "Name: Dwi, Salary: 10000"
## [1] "(Stopped because Nabil has a salary above 10,000)"


  • Python code

# Iterate directly over the list of dictionaries
for emp in employees:

    # Extract employee attributes for current iteration
    name   = emp["name"]   # Get employee name
    salary = emp["salary"] # Get employee salary

    # --- Check if salary strictly exceeds 10,000 ---
    if salary > 10000:
        # Print termination message and exit loop immediately
        print(f"(Stopped because {name} has a salary above 10,000)")
        break  # Exit the loop — remaining employees are not processed
    print(f"Name: {name}, Salary: {salary}")
## Name: Bagas, Salary: 5000
## Name: Joan, Salary: 7000
## Name: Alya, Salary: 6500
## Name: Dwi, Salary: 10000
## (Stopped because Nabil has a salary above 10,000)


Task 4 – Next (Continue)

Explanation

The next keyword in R (equivalent to continue in Python) skips the remainder of the current iteration and proceeds directly to the next one, without terminating the loop entirely. In this task, employees with an “Average” performance rating are excluded from the output while the loop continues processing all remaining records.

The skip logic works as follows:

  • On each iteration, the employee’s performance rating is evaluated
  • If the rating is “Average”, the name is recorded in a collector variable and the current iteration is skipped via next / continue
  • If the rating is not “Average”, the record is printed normally
  • After the loop completes, a second loop prints a dynamic notification for each skipped employee

A collector variable is used instead of a hardcoded name to ensure the program remains robust when the dataset changes:

Language R Python
Skip keyword next continue
Collector variable skipped <- c() skipped = []
Append to collector c(skipped, name) skipped.append(name)
Output formatting paste0() f-string


  • R code

# Use next to skip employees with "Average" performance
skipped <- c()  # Hold skipped names

for (i in seq_len(nrow(employees))) {
  name        <- employees$name[i]
  performance <- employees$performance[i]

  if (performance == "Average") {
    skipped <- c(skipped, name)# Note the name that was skipped
    next
  }

  print(paste0("Name: ", name, ", Performance: ", performance))
}
## [1] "Name: Bagas, Performance: Good"
## [1] "Name: Joan, Performance: Very Good"
## [1] "Name: Dwi, Performance: Good"
## [1] "Name: Nabil, Performance: Very Good"
# Display all dynamically skipped
for (name in skipped) {
  print(paste0("(", name, " is skipped because the performance is 'Average')"))
}
## [1] "(Alya is skipped because the performance is 'Average')"


  • Python code

skipped = []  # Initialize empty list to collect skipped employee names

# Iterate directly over the list of dictionaries
for emp in employees:

    # Extract employee attributes for current iteration
    name        = emp["name"]        # Get employee name
    performance = emp["performance"] # Get performance rating

    # --- Check if performance is "Average" ---
    if performance == "Average":
        skipped.append(name)  # Record skipped employee name dynamically
        continue               # Skip remaining code — move to next iteration

    # Print current record only if performance is not "Average"
    print(f"Name: {name}, Performance: {performance}")
## Name: Bagas, Performance: Good
## Name: Joan, Performance: Very Good
## Name: Dwi, Performance: Good
## Name: Nabil, Performance: Very Good
# --- Print skip notifications after loop completes ---
# Iterates over collected names — works dynamically regardless of dataset changes
for name in skipped:
    print(f'({name} is skipped because the performance is "Average")')
## (Alya is skipped because the performance is "Average")



Conclusion

This practicum demonstrated the practical application of two fundamental control flow structures — conditional statements and loops — using a simple employee dataset implemented in both R and Python.

Several key takeaways can be drawn from this exercise:

While R and Python differ in syntax, their underlying control flow logic is fundamentally equivalent. Understanding both implementations reinforces language-agnostic programming thinking, which is an essential skill in modern data science practice.




References

  1. Syntax and Control Flow from https://bookdown.org/dsciencelabs/data_science_programming/02-Syntax-and-Control-Flow.html