Belajar menggunakan for loop, while loop, break, dan next dalam R menggunakan employee dataset untuk simulasi kasus nyata di dunia kerja.
Setiap karyawan mendapat bonus berdasarkan performa mereka. Very Good = 20%, Good = 10%, Average = 5% dari gaji. Kita gunakan for loop dan if-else untuk menghitung bonus masing-masing karyawan.
# Loop through each employee one by one for (i in 1:length(names)) { # Check performance level to determine bonus percentage if (performance[i] == "Very Good") { # Very Good performance = 20% bonus bonus <- salaries[i] * 0.20 } else if (performance[i] == "Good") { # Good performance = 10% bonus bonus <- salaries[i] * 0.10 } else { # Average performance = only 5% bonus bonus <- salaries[i] * 0.05 } # Display employee name and their calculated bonus cat("Name:", names[i], ", Bonus:", bonus, " ") }
Name: Bagas , Bonus: 500 Name: Joan , Bonus: 1400 Name: Alya , Bonus: 325 Name: Dwi , Bonus: 1000 Name: Nabil , Bonus: 2400
Menggunakan for loop untuk memeriksa setiap karyawan, lalu menampilkan hanya yang gajinya di atas 6.000. Ini adalah contoh filtering data secara manual menggunakan loop.
# Loop through all employees to filter by salary for (i in 1:length(names)) { # Only print employee if salary is greater than 6000 if (salaries[i] > 6000) { # Print name and salary of qualifying employee cat("Name:", names[i], ", Salary:", salaries[i], " ") } # If salary <= 6000, skip and move to next employee }
Name: Joan , Salary: 7000 Name: Alya , Salary: 6500 Name: Dwi , Salary: 10000 Name: Nabil , Salary: 12000
while loop akan terus berjalan selama kondisi terpenuhi. Ketika posisi karyawan adalah Manager, loop berhenti menggunakan break. Berguna saat kita ingin mencari data tertentu lalu berhenti.
# Start index from 1 (first employee) i <- 1 # Keep looping while index is within valid range while (i <= length(names)) { # Print current employee name and position cat("Name:", names[i], ", Position:", positions[i], " ") # If Manager is found, print message and stop the loop if (positions[i] == "Manager") { cat("(Stop here because", names[i], "is a Manager) ") break # Exit while loop immediately } # Manually increment index (while loop does not auto-increment) i <- i + 1 }
Name: Bagas , Position: Staff Name: Joan , Position: Supervisor Name: Alya , Position: Staff Name: Dwi , Position: Manager (Stop here because Dwi is a Manager)
Loop berjalan dan menampilkan karyawan satu per satu. Begitu menemukan karyawan dengan gaji di atas 10.000, loop langsung berhenti menggunakan break sebelum menampilkan nama tersebut.
# Loop through each employee to check salary threshold for (i in 1:length(names)) { # If salary exceeds 10000, show message and stop loop entirely if (salaries[i] > 10000) { cat("(Stopped because", names[i], "has a salary above 10,000) ") break # Remaining employees will NOT be processed } # Only reached if salary is within range (not above 10000) cat("Name:", names[i], ", Salary:", salaries[i], " ") }
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)
next di R berfungsi seperti continue di bahasa lain — melewati iterasi saat ini dan lanjut ke berikutnya. Di sini kita skip karyawan yang performanya Average.
# Loop through each employee to evaluate their performance for (i in 1:length(names)) { # If performance is Average, skip this employee using next if (performance[i] == "Average") { cat("(", names[i], "is skipped because the performance is 'Average') ") next # Jump to next iteration, code below is NOT executed } # Only reached when performance is Good or Very Good cat("Name:", names[i], ", Performance:", performance[i], " ") }
Name: Bagas , Performance: Good Name: Joan , Performance: Very Good ( Alya is skipped because the performance is 'Average') Name: Dwi , Performance: Good Name: Nabil , Performance: Very Good