Data Sains Programming
March 15, 2026
1 Practicum
Independent Practice: Conditional Statements and Loops in Python & R
1.1 Objective
- Understand and implement conditional statements (
if,if-else,if-elif-else). - Apply loops (
for loop,while loop,break,continue) to analyze a dataset.
Use the following dummy dataset:
| 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 |
1.2 Conditional Statements
Determine bonus levels based on employee performance:
- Very Good → 20% of salary
- Good → 10% of salary
- Average → 5% of salary
Your Task: Write a program in R to calculate each employee’s bonus.
# Loop through each employee in the dataset
for(i in 1:nrow(employees)){
# Determine the bonus percentage based on employee performance
if(employees$Performance[i] == "Very Good"){
bonus <- employees$Salary[i] * 0.20
}
else if(employees$Performance[i] == "Good"){
bonus <- employees$Salary[i] * 0.10
}
else{
bonus <- employees$Salary[i] * 0.05
}
# Print the employee name and calculated bonus
cat(paste0("Name: ", employees$Name[i], ", Bonus: ", bonus, "\n"))
}Name: Bagas, Bonus: 500
Name: Joan, Bonus: 1400
Name: Alya, Bonus: 325
Name: Dwi, Bonus: 1000
Name: Nabil, Bonus: 2400
Interpretation: The program uses conditional statements to determine employee bonuses based on their performance levels. Each performance category is associated with a different bonus percentage. During the iteration, the program checks the employee’s performance and applies the corresponding bonus rate to the salary. This process allows the program to automatically calculate the bonus for each employee in the dataset.
1.3 Loops (For & While)
1.3.1 Task 1 — For Loop: Salary > 6000
Use a for loop to list employees with a salary greater than 6000.
# Loop through each employee
for (i in 1:nrow(employees)) {
# Check if salary is greater than 6000
if (employees$Salary[i] > 6000) {
# Display name and salary
cat(paste0("Name: ", employees$Name[i], ", Salary: ", employees$Salary[i], "\n"))
}
}Name: Joan, Salary: 7000
Name: Alya, Salary: 6500
Name: Dwi, Salary: 10000
Name: Nabil, Salary: 12000
Interpretation: The program uses a for loop to iterate through the employee dataset and examine each record. For every employee, the program checks whether the salary is greater than 6000. If the condition is satisfied, the employee’s name and salary are displayed. This demonstrates how loops and conditional logic can be used together to filter specific data from a dataset.
1.3.2 Task 2 — While Loop: Until “Manager”
Use a while loop to display employees until a “Manager” is found.
# Start index
i <- 1
# Loop through employees
while (i <= nrow(employees)) {
# Display employee name and position
cat(paste0("Name: ", employees$Name[i], ", Position: ", employees$Position[i]))
# Stop when Manager is found
if (employees$Position[i] == "Manager") {
cat(" (Stop here)\n")
break
}
cat("\n")
i <- i + 1
}Name: Bagas, Position: Staff
Name: Joan, Position: Supervisor
Name: Alya, Position: Staff
Name: Dwi, Position: Manager (Stop here)
Interpretation: The program uses a while loop to display employee information sequentially from the beginning of the dataset. The loop continues running as long as the position is not “Manager”. When the program encounters an employee with the position “Manager”, the loop stops. This demonstrates how a while loop can control the execution of a program based on a specific condition.
1.3.3 Task 3 — Break: Salary > 10,000
Use break to stop the loop when an employee with a salary above 10,000 is found.
# Loop through employees
for (i in 1:nrow(employees)) {
# Stop if salary is above 10000
if (employees$Salary[i] > 10000) {
cat(paste0("(Stopped because ", employees$Name[i], " has a salary above 10,000)\n"))
break
}
# Display employee name and salary
cat(paste0("Name: ", employees$Name[i], ", Salary: ", employees$Salary[i], "\n"))
}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)
Interpretation: The program uses the break statement to terminate the loop when a specific condition is met. In this case, the loop stops when an employee with a salary above 10,000 is found. Once the condition is satisfied, the program immediately exits the loop and does not continue to the remaining data. This shows how break can be used to control the execution of a loop efficiently.
1.3.4 Task 4 — Next: Skip “Average”
Use next to skip employees with “Average” performance.
# Variable to store output
output_text <- ""
# Loop through employees
for (i in 1:nrow(employees)) {
# Skip employees with Average performance
if (employees$Performance[i] == "Average") {
skipped_name <- employees$Name[i]
next
}
# Add result to text
output_text <- paste0(
output_text,
"Name: ", employees$Name[i],
", Performance: ", employees$Performance[i],
"\n"
)
}
# Add skipped message
output_text <- paste0(
output_text,
"(", skipped_name,
" is skipped because the performance is \"Average\")"
)
# Print all results once
cat(output_text)Name: Bagas, Performance: Good
Name: Joan, Performance: Very Good
Name: Dwi, Performance: Good
Name: Nabil, Performance: Very Good
(Alya is skipped because the performance is "Average")
Interpretation: The program uses the next statement to skip employees whose performance is classified as “Average”. When this condition occurs, the program ignores that record and continues to the next iteration.
2 Conclusion
This practicum demonstrates how conditional statements and loops can be used to process and analyze structured data. By applying if–else conditions, the program can make logical decisions, such as determining employee bonuses based on performance. In addition, loops like for and while allow the program to iterate through the dataset efficiently. Control statements such as break and continue (or next in R) help manage the flow of iteration when specific conditions occur. Overall, these programming concepts are important in data science because they help automate data processing and implement logical analysis.
3 References
Siregar, B. (2024). Data Science Programming: Syntax and Control Flow. https://bookdown.org/dsciencelabs/data_science_programming/02-Syntax-and-Control-Flow.html