Practicum 2 — Syntax & Control Flow
Home
Adinda
Adinda Adelia Futri
Student ID: 52250055  ·  Program: Data Science
Institution: Institut Teknologi Sains Bandung
Practicum 2 — Syntax and Control Flow · R Language
R Language Conditional For Loop While Loop Break Continue
📋 Employee Dataset — Dummy Data
IDNameAgeSalaryPositionPerformance
1Bagas255,000StaffGood
2Joan307,000SupervisorVery Good
3Alya276,500StaffAverage
4Dwi3510,000ManagerGood
5Nabil4012,000DirectorVery Good
💰 Question 1 — Conditional Statements: Employee Bonus
Calculate bonus based on performance: Very Good → 20% · Good → 10% · Average → 5% of salary.
R CodeR
employees <- data.frame( ID = c(1,2,3,4,5), Name = c("Bagas","Joan","Alya","Dwi","Nabil"), Salary = c(5000,7000,6500,10000,12000), Performance = c("Good","Very Good", "Average","Good","Very Good") ) for (i in 1:nrow(employees)) { name <- employees$Name[i] salary <- employees$Salary[i] perf <- employees$Performance[i] # Determine bonus % based on performance if (perf == "Very Good") { pct <- 0.20 } else if (perf == "Good") { pct <- 0.10 } else { pct <- 0.05 } bonus <- salary * pct cat(sprintf("Name: %s, Bonus: %.0f\n", name, bonus)) }
OutputConsole
Name: Bagas, Bonus: 500 Name: Joan, Bonus: 1400 Name: Alya, Bonus: 325 Name: Dwi, Bonus: 1000 Name: Nabil, Bonus: 2400
📊 Bonus Visualization per Employee
0 500 1000 1500 500 Bagas 1400 Joan 325 Alya 1000 Dwi 2400 Nabil
💡 Interpretation
Nabil receives the highest bonus of 2,400 (Very Good, 20% of 12,000). Joan follows with 1,400. Alya gets the smallest bonus of 325 due to Average performance (5%). The if-else if-else conditional successfully classifies each employee into the correct bonus tier.
🔁 Question 2 — For Loop: Employees with Salary > 6,000
Use a for loop to display employees who have a salary greater than 6,000.
R CodeR
# For loop: filter employees with salary > 6000 for (i in 1:nrow(employees)) { name <- employees$Name[i] salary <- employees$Salary[i] # Condition: only display if salary > 6000 if (salary > 6000) { cat(sprintf("Name: %s, Salary: %d\n", name, salary)) } }
OutputConsole
Name: Joan, Salary: 7000 Name: Alya, Salary: 6500 Name: Dwi, Salary: 10000 Name: Nabil, Salary: 12000
📊 All Employee Salaries (threshold 6,000)
6,000 0 6K 12K 5K Bagas 7K Joan 6.5K Alya 10K Dwi 12K Nabil
💡 Interpretation
Out of 5 employees, 4 employees have a salary above 6,000 (green bars). Only Bagas does not meet the criteria because his salary is 5,000. The for loop iterates through all rows one by one and the condition if (salary > 6000) works as an effective filter.
Question 3 — While Loop: Display Until Manager
Use a while loop to display employees one by one until the position "Manager" is found (inclusive).
R CodeR
i <- 1 while (i <= nrow(employees)) { name <- employees$Name[i] position <- employees$Position[i] # If Manager found: display then stop if (position == "Manager") { cat(sprintf( "Name: %s, Position: %s (Stop here)\n", name, position)) break } cat(sprintf("Name: %s, Position: %s\n", name, position)) i <- i + 1 }
OutputConsole
Name: Bagas, Position: Staff Name: Joan, Position: Supervisor Name: Alya, Position: Staff Name: Dwi, Position: Manager (Stop here)
📊 While Loop Flow — Iteration per Employee
i=1 · Bagas Staff → continue i=2 · Joan Supervisor → continue i=3 · Alya Staff → continue i=4 · Dwi Manager → BREAK ✕ i=5 · Nabil (not reached)
💡 Interpretation
The while loop stops at iteration 4 (Dwi) because his position is "Manager". The loop successfully processes 4 out of 5 employees. Nabil is never reached because break terminates the loop before reaching row 5. This demonstrates the efficiency of a while loop for sequential searching.
Question 4 — Break: Stop When Next Salary > 10,000
Use break to stop the loop when the next employee has a salary above 10,000.
R CodeR
for (i in 1:nrow(employees)) { name <- employees$Name[i] salary <- employees$Salary[i] # Check the NEXT employee's salary if (i < nrow(employees) && employees$Salary[i+1] > 10000) { cat(sprintf("Name: %s, Salary: %d\n", name, salary)) cat(sprintf( "(Stopped because %s has salary > 10,000)\n", employees$Name[i+1])) break } cat(sprintf("Name: %s, Salary: %d\n", name, salary)) }
OutputConsole
Name: Bagas, Salary: 5000 Name: Joan, Salary: 7000 Name: Alya, Salary: 6500 Name: Dwi, Salary: 10000 (Stopped because Nabil has salary > 10,000)
📊 Break Position Within Loop Iteration
Bagas 5K displayed Joan 7K displayed Alya 6.5K displayed Dwi 10K break! STOP Nabil 12K not reached Nabil[i+1].salary=12K > 10K → break at Dwi
💡 Interpretation
The loop stops while processing Dwi because the program detects that the next employee (Nabil, 12,000) has a salary above 10,000. break immediately terminates the loop without processing Nabil at all. A total of 4 out of 5 employees are successfully displayed.
⏭️ Question 5 — Continue (next): Skip "Average" Performance
Use next to skip employees with "Average" performance without stopping the loop.
R CodeR
for (i in 1:nrow(employees)) { name <- employees$Name[i] perf <- employees$Performance[i] # Skip iteration if performance is Average if (perf == "Average") next cat(sprintf("Name: %s, Performance: %s\n", name, perf)) } cat("(Alya skipped — performance is \"Average\")\n")
OutputConsole
Name: Bagas, Performance: Good Name: Joan, Performance: Very Good Name: Dwi, Performance: Good Name: Nabil, Performance: Very Good (Alya skipped — performance is "Average")
📊 Iteration Status per Employee
✓ Bagas Good → displayed ✓ Joan Very Good → displayed ⏭ Alya Average → next (skip) ✓ Dwi Good → displayed ✓ Nabil Very Good → displayed
💡 Interpretation
next skips Alya (Average performance) without stopping the loop — the loop continues to Dwi and Nabil. Unlike break, next only skips a single iteration. A total of 4 out of 5 employees are displayed; Alya is skipped because she does not meet the performance criteria.