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
| 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 |
💰 Question 1 — Conditional Statements: Employee Bonus
Calculate bonus based on performance: Very Good → 20% · Good → 10% · Average → 5% of salary.
Calculate bonus based on performance: Very Good → 20% · Good → 10% · Average → 5% of salary.
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))
}
Name: Bagas, Bonus: 500
Name: Joan, Bonus: 1400
Name: Alya, Bonus: 325
Name: Dwi, Bonus: 1000
Name: Nabil, Bonus: 2400
📊 Bonus Visualization per Employee
💡 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
Use a
for loop to display employees who have a salary greater than 6,000.
# 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))
}
}
Name: Joan, Salary: 7000
Name: Alya, Salary: 6500
Name: Dwi, Salary: 10000
Name: Nabil, Salary: 12000
📊 All Employee Salaries (threshold 6,000)
💡 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
Use a
while loop to display employees one by one until the position "Manager" is found (inclusive).
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
}
Name: Bagas, Position: Staff
Name: Joan, Position: Supervisor
Name: Alya, Position: Staff
Name: Dwi, Position: Manager (Stop here)
📊 While Loop Flow — Iteration per Employee
💡 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
Use
break to stop the loop when the next employee has a salary above 10,000.
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))
}
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
💡 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
Use
next to skip employees with "Average" performance without stopping the loop.
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")
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
💡 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.