Tugas Prakticum Week-4

Syntax and Control Flow

Boma Satrio
Wicaksono Dewantoro

Junior Data Scientist
Institut Teknologi Sains Bandung (ITSB)
NIM: 52250061
CREATIVITY
DATA SCIENCE
PROGRAMMING

Introduction

In data science and programming, understanding how a program controls the flow of execution is an important fundamental skill. Control flow allows a program to make decisions, repeat tasks, and manage different conditions within a dataset.

This practicum demonstrates the use of basic control flow structures using the R programming language. Several programming concepts are implemented, including conditional statements, for loops, while loops, and control statements such as break and continue.

Using a simple employee dataset containing information about names, salaries, positions, and performance, this exercise aims to illustrate how programming logic can be applied to process and analyze structured data effectively.

Database

library(knitr)

employees <- data.frame(
  Name = c("Bagas","Joan","Alya","Dwi","Nabil"),
  Salary = c(5000,7000,6500,10000,12000),
  Performance = c("Good","Very Good","Average","Good","Very Good"),
  Position = c("Staff","Supervisor","Staff","Manager","Director")
)

kable(employees, caption = "Database Karyawan")
Database Karyawan
Name Salary Performance Position
Bagas 5000 Good Staff
Joan 7000 Very Good Supervisor
Alya 6500 Average Staff
Dwi 10000 Good Manager
Nabil 12000 Very Good Director

2.5.2 Conditional Statements (Bonus Performance)

Objective

To demonstrate how conditional statements can be used to calculate employee bonuses based on their performance levels.

Instructions

Create a conditional statement to calculate the employee bonus based on their performance.

Bonus rules:

  • Performance Very Good → 20% of salary
  • Performance Good → 10% of salary
  • Performance Average → 5% of salary
for (i in 1:nrow(employees)) {
  
  performance <- employees$Performance[i]
  salary <- employees$Salary[i]
  name <- employees$Name[i]
  
  if (performance == "Very Good") {
    bonus <- salary * 0.20
    
  } else if (performance == "Good") {
    bonus <- salary * 0.10
    
  } else {
    bonus <- salary * 0.05
  }
  
  cat("Name:", name, "| Bonus:", bonus, "\n")
}
## Name: Bagas | Bonus: 500 
## Name: Joan | Bonus: 1400 
## Name: Alya | Bonus: 325 
## Name: Dwi | Bonus: 1000 
## Name: Nabil | Bonus: 2400

2.5.3 Loops Task 1: Salary > 6000

Objective

To demonstrate how a for loop can be used to iterate through a dataset and filter employees based on salary conditions.

Instructions

Use a for loop to go through the employee dataset and display employees whose salary is greater than 6000.

for (i in 1:nrow(employees)) {
  
  name <- employees$Name[i]
  salary <- employees$Salary[i]
  
  if (salary > 6000) {
    cat("Name:", name, "| Salary:", salary, "\n")
  }
}
## Name: Joan | Salary: 7000 
## Name: Alya | Salary: 6500 
## Name: Dwi | Salary: 10000 
## Name: Nabil | Salary: 12000

2.5.3 Loops Task 2: Find Manager (While Loop)

Objective

To demonstrate how a while loop repeatedly executes a task while a condition remains true.

Instructions

Use a while loop to display employee data sequentially until all employee records in the dataset have been processed.

i <- 1

while (i <= nrow(employees)) {
  
  name <- employees$Name[i]
  position <- employees$Position[i]
  
  if (position == "Manager") {
    cat("Name:", name, "| Position:", position, "| Stop here\n")
    break
  } else {
    cat("Name:", name, "| Position:", position, "\n")
  }
  
  i <- i + 1
}
## Name: Bagas | Position: Staff 
## Name: Joan | Position: Supervisor 
## Name: Alya | Position: Staff 
## Name: Dwi | Position: Manager | Stop here

2.5.3 Loops Task 3: Break if Salary > 10,000

Objective

To demonstrate how the break statement can be used to stop a loop when a specific condition is met.

Instructions

Display employee data until a salary greater than 10,000 is encountered.

If a salary greater than 10,000 is found:

  • display the previous employee data
  • add the text “Stop here”
  • stop the loop execution
for (i in 1:nrow(employees)) {

  name <- employees$Name[i]
  salary <- employees$Salary[i]

  if (salary > 10000) {

    prev_name <- employees$Name[i-1]
    prev_salary <- employees$Salary[i-1]

    cat("Name:", prev_name, "| Salary:", prev_salary, "| Stop here\n")
    cat("(Stopped because", name, "has a salary above 10,000)\n")

    break
  }

  if (salary <= 10000) {
    cat("Name:", name, "| Salary:", salary, "\n")
  }

}
## Name: Bagas | Salary: 5000 
## Name: Joan | Salary: 7000 
## Name: Alya | Salary: 6500 
## Name: Dwi | Salary: 10000 
## Name: Dwi | Salary: 10000 | Stop here
## (Stopped because Nabil has a salary above 10,000)

2.5.3 Loops Task 4: Continue (Skip Average)

Objective

To demonstrate how the continue (next) statement can be used to skip specific data during loop execution.

Instructions

Display employee data but skip employees whose performance is Average.

If an employee has Average performance, skip the employee and display a message explaining that the employee was skipped.

for (i in 1:nrow(employees)) {

  name <- employees$Name[i]
  performance <- employees$Performance[i]

  if (performance == "Average") {
    cat("(", name, "Skipped because performance is Average)\n")
    next
  }

  cat("Name:", name, "| Performance:", performance, "\n")

}
## Name: Bagas | Performance: Good 
## Name: Joan | Performance: Very Good 
## ( Alya Skipped because performance is Average)
## Name: Dwi | Performance: Good 
## Name: Nabil | Performance: Very Good

Conclusion

This practicum demonstrates the implementation of fundamental control flow concepts using the R programming language. Through several programming tasks, the exercise illustrates how conditional statements, loops, and control statements can be used to manage the flow of execution within a program.

The conditional statement was used to calculate employee bonuses based on their performance levels. The for loop was applied to iterate through the dataset and filter employees based on salary conditions. Meanwhile, the while loop showed how iteration can continue until a specific condition is met. Control statements such as break and continue were also implemented to stop the loop under certain conditions or skip specific data entries.

Overall, these programming techniques are essential for processing structured datasets and building logical workflows in data analysis. Understanding these basic programming structures is an important foundation for further studies in data science and statistical computing.

References