Syntax and Control Flow
Practicum ~ Week 4
M. Yustian Putra Muhadi
Institut Teknologi Sains Bandung
Lectur: Mr. Bakti Siregar, M.Sc., CDS
Learning Objectives
Understand and apply conditional statements (if, if-else, if-else if-else).
Use loops (for loop, while loop, break, next) to analyze a dataset.
1 Dataset
# Membuat data frame dari tabel karyawan
data <- data.frame(
ID = c(1, 2, 3, 4, 5),
Name = c("Bagas", "Joan", "Alya", "Dwi", "Nabil"),
Age = c(25, 30, 27, 35, 40),
Salary = c(5000, 7000, 6500, 10000, 12000),
Position = c("Staff", "Supervisor", "Staff", "Manager", "Director"),
Performance = c("Good", "Very Good", "Average", "Good", "Very Good")
)
print(data)## ID Name Age Salary Position Performance
## 1 1 Bagas 25 5000 Staff Good
## 2 2 Joan 30 7000 Supervisor Very Good
## 3 3 Alya 27 6500 Staff Average
## 4 4 Dwi 35 10000 Manager Good
## 5 5 Nabil 40 12000 Director Very Good
Employee Dataset
5 employees · 6 variables
| ID | Name | Age | Salary | Position | Performance |
|---|---|---|---|---|---|
| 1 | Bagas | 25 | $5,000 | Staff | Good |
| 2 | Joan | 30 | $7,000 | Supervisor | Very Good |
| 3 | Alya | 27 | $6,500 | Staff | Average |
| 4 | Dwi | 35 | $10,000 | Manager | Good |
| 5 | Nabil | 40 | $12,000 | Director | Very Good |
Interpretation: This code builds the foundation for analysis by organizing employee data into a structured data frame. The table provides an immediate overview of six variables — ID, name, age, salary, position, and performance — which are crucial before performing further operations such as calculations or filtering. This step reflects standard practice in data programming, where data must be available in a structured format for efficient processing.
2 Conditional Statements
Task: Determine the bonus level based on employee performance:
- Very Good → 20% of salary
- Good → 10% of salary
- Average → 5% of salary
# Menghitung bonus berdasarkan performance
for (i in 1:nrow(data)) {
karyawan <- data[i, ]
if (karyawan$Performance == "Very Good") {
bonus <- karyawan$Salary * 0.20
} else if (karyawan$Performance == "Good") {
bonus <- karyawan$Salary * 0.10
} else { # Average
bonus <- karyawan$Salary * 0.05
}
cat(paste0("Name: ", karyawan$Name, ", Bonus: ", as.integer(bonus), "\n"))
}## Name: Bagas, Bonus: 500
## Name: Joan, Bonus: 1400
## Name: Alya, Bonus: 325
## Name: Dwi, Bonus: 1000
## Name: Nabil, Bonus: 2400
Bonus Calculator — if / else if / else
Interpretation: Branching logic (if-else) automatically determines different bonus percentages for each performance level: 20% for "Very Good," 10% for "Good," and 5% for "Average." This demonstrates how business decisions can be translated into code, ensuring consistent calculations and avoiding manual errors. Results are displayed in an informative format for easy management review.
3 Loops (For & While)
Loops Overview: Use loops to analyze the employee dataset using four techniques:
- for loop — iterate over all employees to filter by salary
- while loop — iterate until a condition is met
- break — exit a loop early when a condition triggers
- next — skip an iteration and continue to the next
3.1 For Loop
Task: Use a for loop to display employees with salary greater than 6,000.
# Menggunakan for loop untuk menampilkan karyawan dengan gaji > 6000
cat("Karyawan dengan gaji di atas 6000:\n")## Karyawan dengan gaji di atas 6000:
for (i in 1:nrow(data)) {
if (data$Salary[i] > 6000) {
cat(paste0("Name: ", data$Name[i], ", Salary: ", data$Salary[i], "\n"))
}
}## Name: Joan, Salary: 7000
## Name: Alya, Salary: 6500
## Name: Dwi, Salary: 10000
## Name: Nabil, Salary: 12000
Interpretation: Using a for loop, we filter employees with salaries above 6,000 by iterating each row and applying an if condition. This technique quickly identifies high-paid employees for further evaluation or policy decisions.
3.2 While Loop
Task: Use a while loop to display employees until a “Manager” is found, then stop.
# Menggunakan while loop sampai ditemukan posisi Manager
cat("Menampilkan karyawan sampai ditemukan Manager:\n")## Menampilkan karyawan sampai ditemukan Manager:
i <- 1
while (i <= nrow(data)) {
karyawan <- data[i, ]
if (karyawan$Position == "Manager") {
cat(paste0("Name: ", karyawan$Name, ", Position: ", karyawan$Position, " (Stop here)\n"))
break
} else {
cat(paste0("Name: ", karyawan$Name, ", Position: ", karyawan$Position, "\n"))
}
i <- i + 1
}## Name: Bagas, Position: Staff
## Name: Joan, Position: Supervisor
## Name: Alya, Position: Staff
## Name: Dwi, Position: Manager (Stop here)
Interpretation: The while loop processes employees sequentially, displaying each name and position until the "Manager" position is encountered. This reflects real-world scenarios like queue processing where data is handled up to a specific point based on dynamic conditions.
3.3 Break
Task: Use break to stop the loop immediately when a salary above 10,000 is found.
# Menggunakan break ketika menemukan gaji di atas 10000
cat("Menampilkan karyawan hingga menemukan gaji di atas 10,000:\n")## Menampilkan karyawan hingga menemukan gaji di atas 10,000:
for (i in 1:nrow(data)) {
if (data$Salary[i] > 10000) {
cat(paste0("(Stopped because ", data$Name[i], " has a salary above 10,000)\n"))
break
} else {
cat(paste0("Name: ", data$Name[i], ", Salary: ", data$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 break command halts the loop immediately upon finding a salary above 10,000, preventing unnecessary processing. This technique is useful in data retrieval when we only need to process records up to a specific threshold or condition.
3.4 Continue (Next)
Task: Use next to skip employees with “Average” performance and continue to the next iteration.
# Menggunakan continue untuk melewatkan karyawan dengan performance "Average"
cat("Karyawan dengan performa (selain Average):\n")## Karyawan dengan performa (selain Average):
skipped <- c()
for (i in 1:nrow(data)) {
if (data$Performance[i] == "Average") {
skipped <- c(skipped, data$Name[i])
next
}
cat(paste0("Name: ", data$Name[i], ", Performance: ", data$Performance[i], "\n"))
}## Name: Bagas, Performance: Good
## Name: Joan, Performance: Very Good
## Name: Dwi, Performance: Good
## Name: Nabil, Performance: Very Good
if (length(skipped) > 0) {
cat(paste0("(", paste(skipped, collapse = ", "),
ifelse(length(skipped) == 1, " is ", " are "),
"skipped because the performance is \"Average\")\n"))
}## (Alya is skipped because the performance is "Average")
Interpretation: The next command skips "Average" employees and records their names, displaying only those with "Good" and "Very Good" performance. This elegantly filters data without altering the original structure, and the omitted names are automatically reported at the end for transparency.