Syntax and Control Flow
1 Dataset
| ID | Name | Salary | Performance | Position |
|---|---|---|---|---|
| 1 | Bagas | 5000 | Good | Staff |
| 2 | Joan | 7000 | Very Good | Supervisor |
| 3 | Alya | 6500 | Average | Staff |
| 4 | Dwi | 10000 | Good | Manager |
| 5 | Nabil | 12000 | Very Good | Director |
2 Bonus Levels
From that Dataset, we calculate the employee bonus based on their performance. Bonus rules:
- Performance Very Good -> 20% of salary
- Performance Good -> 10% of salary
- Performance Average -> 5% of salary
for (i in 1:nrow(employees)) { # menghitung jumlah baris
if (employees$Performance[i] == "Very Good") { # Jika "performance = very good" dari data employees
employees$Bonus[i] <- employees$Salary[i] * 0.20 # bonus = salary dikali 20%
}
else if (employees$Performance[i] == "Good") { # Jika "performance = good" dari data employees
employees$Bonus[i] <- employees$Salary[i] * 0.10 # bonus = salary dikali 10%
}
else {
employees$Bonus[i] <- employees$Salary[i] * 0.05 # Salary dikali 5% jika performance selain keduanya
}
cat(paste0("Name: ", employees$Name[i], ", Bonus: ", employees$Bonus[i], "\n")) # paste0 untuk menggabungkan semua teks jadi satu string
}## Name: Bagas, Bonus: 500
## Name: Joan, Bonus: 1400
## Name: Alya, Bonus: 325
## Name: Dwi, Bonus: 1000
## Name: Nabil, Bonus: 2400
| Name | Salary | Bonus | Total_Salary |
|---|---|---|---|
| Bagas | 5000 | 500 | 5500 |
| Joan | 7000 | 1400 | 8400 |
| Alya | 6500 | 325 | 6825 |
| Dwi | 10000 | 1000 | 11000 |
| Nabil | 12000 | 2400 | 14400 |
3 For Loop
Use a for loop to list employees with a salary greater than 6000
for (i in 1:nrow(employees)) {
if (employees$Salary[i] > 6000) { # jika salary > 6000 dari data employees
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
4 While Loop
Use a while loop to display employees until a manager is Found
i <- 1
manager_found <- FALSE # kondisi manager belum ditemukam
while (i <= nrow(employees) & manager_found == FALSE) {
if (employees$Position[i] == "Manager") {
cat("Name:", employees$Name[i], ", Position:", employees$Position[i], "(Stop here)\n")
manager_found <- TRUE # loop otomatis berhenti karena manager telah ditemukan
} else {
cat(paste0("Name: ", employees$Name[i], ", Position: ", employees$Position[i], "\n"))
}
i <- i + 1
}## Name: Bagas, Position: Staff
## Name: Joan, Position: Supervisor
## Name: Alya, Position: Staff
## Name: Dwi , Position: Manager (Stop here)
5 Break
to stop the loop when an employee with a salary above 10,000 is found
for (i in 1:nrow(employees)) {
if (employees$Salary[i] > 10000) { # kondisi jika salary > 10,000
cat("(Stopped because", employees$Name[i], "has a salary above 10,000)\n") # output jika salary > 10,000
break # menghentikan loop karena kondisi terpenuhi
}
cat(paste0("Name: ", employees$Name[i], ", Salary: ", employees$Salary[i], "\n")) # output jika salary <= 10,000
}## 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)
6 Continue
to skip employees with Average performance
for (i in 1:nrow(employees)) {
if (employees$Performance[i] == "Average") {
cat("(Skipped because", employees$Name[i], "has an Average performance)\n")
next # skip baris, loop tetap berjalan
}
cat(paste0("Name: ", employees$Name[i], ", Performance: ", employees$Performance[i], "\n"))
} ## Name: Bagas, Performance: Good
## Name: Joan, Performance: Very Good
## (Skipped because Alya has an Average performance)
## Name: Dwi, Performance: Good
## Name: Nabil, Performance: Very Good