Practice: Conditional Statement and Loops

Assignment Week 4

March 16, 2026

Profile Photo

KAYLA APRILIA

Data Science Student at ITSB

NIM: 52250057

Email: kaylaaprilia2142@gmail.com

R Programming Data Science Statistics

1 Dataset

The following dataset represents a sample list of employees that will be used to demonstrate conditional statements and looping structures in programming.

# Load library
library(knitr)

# Create employee dataset
employees <- data.frame(
  ID = c(1,2,3,4,5),
  Name = c("Bagas","Joan","Alya","Dwi","Nabil"),
  Age = c(25,30,27,35,40),
  Salary = c(5000,7000,6500,10000,12000),
  Position = c("Staff","Supervisor","Staff","Manager","Director"),
  Performance = c("Good","Excellent","Average","Good","Excellent")
)

# Display dataset
kable(employees, caption = "Employee Dataset", align = "c")
Employee Dataset
ID Name Age Salary Position Performance
1 Bagas 25 5000 Staff Good
2 Joan 30 7000 Supervisor Excellent
3 Alya 27 6500 Staff Average
4 Dwi 35 10000 Manager Good
5 Nabil 40 12000 Director Excellent

2 Conditional Statement

A conditional statement is used in programming to make decisions by executing different actions based on whether a certain condition is true or false. Common conditional statements include if, elif/else if, and else, which allow a program to check conditions and determine which block of code should run. In the tasks, conditional statements are used to filter employees based on salary, position, and performance, as well as to calculate bonuses according to performance levels.


Determine bonus levels based on employee performance:

  • Very Good -> 20% of salary
  • Good -> 10% of salary
  • Average -> 5% of salary

Task: Write a program in R to calculate each employee’s bonus.

# Employee dataset
name <- c("Bagas","Joan","Alya","Dwi","Nabil")
salary <- c(5000,7000,6500,10000,12000)
performance <- c("Good","Excellent","Average","Good","Excellent")

employees <- data.frame(name, salary, performance)

# Loop through each employee
for (i in 1:nrow(employees)) {
  
  # Determine bonus based on performance
  if (employees$performance[i] == "Excellent") {
    bonus <- employees$salary[i] * 0.20
  } else if (employees$performance[i] == "Good") {
    bonus <- employees$salary[i] * 0.10
  } else if (employees$performance[i] == "Average") {
    bonus <- employees$salary[i] * 0.05
  }
  
  # Display name and bonus
  cat("Name:", employees$name[i], ", Bonus:", bonus, "\n")
}
## Name: Bagas , Bonus: 500 
## Name: Joan , Bonus: 1400 
## Name: Alya , Bonus: 325 
## Name: Dwi , Bonus: 1000 
## Name: Nabil , Bonus: 2400

3 Loops (For & While)

Loops are used in programming to repeat a set of instructions multiple times until a certain condition is met. They help process data efficiently without writing the same code repeatedly.

In the tasks, loops are used to iterate through an employee dataset to perform different operations. For example, a for loop is used to go through each employee and display those with a salary greater than 6000. A while loop repeats the process until a specific condition is met, such as stopping after a Manager is found. Control statements like break are used to stop the loop when a condition occurs, while continue (or next in R) skips certain data, such as employees with Average performance.


3.1 For Loop

A for loop is used to iterate over a sequence of elements, such as a list, array, or dataset, and execute a block of code for each element. It is commonly used when the number of iterations is known or when processing each item in a collection one by one.

Task: Use a for loop to display employees whose salary is greater than 6000.

# Create employee dataset
name <- c("Bagas","Joan","Alya","Dwi","Nabil")
age <- c(25,30,27,35,40)
salary <- c(5000,7000,6500,10000,12000)
position <- c("Staff","Supervisor","Staff","Manager","Director")
performance <- c("Good","Excellent","Average","Good","Excellent")

employees <- data.frame(name, age, salary, position, performance)

# Loop through each row of the dataset
for (i in 1:nrow(employees)) {
  
  # Check salary condition
  if (employees$salary[i] > 6000) {
    
    # Print name and salary
    cat("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

3.2 While Loop

A while loop repeatedly executes a block of code as long as a specified condition remains true. It is typically used when the number of iterations is not predetermined and the loop continues until a certain condition changes.

Task: Use a while loop to display employees until a “Manager” is found.

# Create employee dataset
name <- c("Bagas","Joan","Alya","Dwi","Nabil")
position <- c("Staff","Supervisor","Staff","Manager","Director")

employees <- data.frame(name, position)

# Start index
i <- 1

# While loop
while (i <= nrow(employees)) {
  
  # Print employee name and position
  cat("Name:", employees$name[i], ", Position:", employees$position[i], "\n")
  
  # Stop loop if Manager is found
  if (employees$position[i] == "Manager") {
    break
  }
  
  # Move to next employee
  i <- i + 1
  
}
## Name: Bagas , Position: Staff 
## Name: Joan , Position: Supervisor 
## Name: Alya , Position: Staff 
## Name: Dwi , Position: Manager

3.3 Break

The break statement is used to immediately stop a loop when a specific condition is met. Once break is executed, the program exits the loop and continues running the code after the loop.

Task: Use break to stop the loop when an employee with a salary above 10,000 is found.

# Employee dataset
name <- c("Bagas","Joan","Alya","Dwi","Nabil")
salary <- c(5000,7000,6500,10000,12000)

employees <- data.frame(name, salary)

# Loop through each employee
for (i in 1:nrow(employees)) {
  
  # Stop the loop if salary is above 10000
  if (employees$salary[i] > 10000) {
    break
  }
  
  # Print name and salary
  cat("Name:", employees$name[i], ", Salary:", employees$salary[i], "\n")
  
}
## Name: Bagas , Salary: 5000 
## Name: Joan , Salary: 7000 
## Name: Alya , Salary: 6500 
## Name: Dwi , Salary: 10000

3.4 Continue

The continue statement is used to skip the current iteration of a loop and move directly to the next iteration. It does not stop the loop completely but simply ignores the remaining code for the current cycle.

Task: Use continue to skip employees with “Average” performance.

# Employee dataset
name <- c("Bagas","Joan","Alya","Dwi","Nabil")
performance <- c("Good","Excellent","Average","Good","Excellent")

employees <- data.frame(name, performance)

# Loop through each employee
for (i in 1:nrow(employees)) {
  
  # Skip employees with "Average" performance
  if (employees$performance[i] == "Average") {
    next
  }
  
  # Print name and performance
  cat("Name:", employees$name[i], ", Performance:", employees$performance[i], "\n")
  
}
## Name: Bagas , Performance: Good 
## Name: Joan , Performance: Excellent 
## Name: Dwi , Performance: Good 
## Name: Nabil , Performance: Excellent

4 Conclusion

The results show that Joan, Alya, Dwi, and Nabil have salaries greater than 6000. The loop stops after reaching Dwi, who holds the Manager position. Employees with Average performance are skipped, so Alya is not displayed in that task. The loop with the break condition stops before Nabil because his salary is above 10,000. In the bonus calculation, Bagas receives 500, Joan receives 1400, Alya receives 325, Dwi receives 1000, and Nabil receives 2400 based on their performance levels.


5 Reference

Data Science Labs. (n.d.). Syntax and control flow: Loops. In Data Science Programming. Retrieved from https://bookdown.org/dsciencelabs/data_science_programming/02-Syntax-and-Control-Flow.html#loops