Practicum Week 4

Ahmad Rizki Mubarak

2026-03-16

Practicum Week 4 - Ahmad Rizki Mubarak
Ahmad Rizki Mubarak
// STUDENT
Ahmad Rizki Mubarak
NIM: 52250036 Data Science
PRACTICUM // WEEK 4

LOOPS &
CONDITIONALS
IN R

Belajar menggunakan for loop, while loop, break, dan next dalam R menggunakan employee dataset untuk simulasi kasus nyata di dunia kerja.

📦 Employee Dataset
Bagas
Usia 25
Gaji 5,000
Posisi Staff
Good
Joan
Usia 30
Gaji 7,000
Posisi Supervisor
Very Good
Alya
Usia 27
Gaji 6,500
Posisi Staff
Average
Dwi
Usia 35
Gaji 10,000
Posisi Manager
Good
Nabil
Usia 40
Gaji 12,000
Posisi Director
Very Good
💻 Exercises
01
IF / ELSE INSIDE FOR LOOP
Hitung Bonus Berdasarkan Performa
â–¾

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.

R CODE
# 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, "
")
}
â–¶ Output
Name: Bagas , Bonus: 500
Name: Joan , Bonus: 1400
Name: Alya , Bonus: 325
Name: Dwi , Bonus: 1000
Name: Nabil , Bonus: 2400
02
FOR LOOP + IF CONDITION
Tampilkan Karyawan dengan Gaji > 6.000
â–¾

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.

R CODE
# 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
}
â–¶ Output
Name: Joan , Salary: 7000
Name: Alya , Salary: 6500
Name: Dwi , Salary: 10000
Name: Nabil , Salary: 12000
03
WHILE LOOP + BREAK
Berhenti Saat Menemukan Manager
â–¾

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.

R CODE
# 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
}
â–¶ Output
Name: Bagas , Position: Staff
Name: Joan , Position: Supervisor
Name: Alya , Position: Staff
Name: Dwi , Position: Manager
(Stop here because Dwi is a Manager)
04
FOR LOOP + BREAK
Berhenti Saat Gaji di Atas 10.000
â–¾

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.

R CODE
# 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], "
")
}
â–¶ Output
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)
05
FOR LOOP + NEXT
Lewati Karyawan dengan Performa Average
â–¾

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.

R CODE
# 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], "
")
}
â–¶ Output
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