ULIN NIKMAH (52250042)
INSTITUT TEKNOLOGI SAINS BANDUNG
Course:Data Science Programming Study Program:Data Science Lecturer:Bakti Siregar, M.SC., CDS.
Control flow is an important concept in programming because it determines how a program executes instructions. By using control flow, a program can make decisions and repeat certain operations automatically.
In this practicum, several basic control flow structures are applied, including:
A simple dataset about employees is used to demonstrate how these concepts work in programming.
# read CSV dataset
employee_data <- read.csv("C:/Users/nulin/OneDrive/Lampiran/SEMESTER 2 PROGRAMING SAINS DATA/Assigment Week 4/DATASET WEEK 4.CSV")
# display dataset as table
knitr::kable(employee_data)
| ID | Name | Age | Salary | Position | Performance |
|---|---|---|---|---|---|
| 1 | Bagas | 25 | 5000 | Staff | Good |
| 2 | Joan | 30 | 7000 | Supervisor | Very good |
| 3 | Alya | 27 | 6500 | Staff | Average |
| 4 | Dwi | 35 | 10000 | Manager | Good |
| 5 | Nabil | 40 | 12000 | Director | Very good |
name <- employee_data$Name
salary <- employee_data$Salary
performance <- employee_data$Performance
position <- employee_data$Position
Conditional statements are used to make decisions in a program based on certain conditions. In this example, conditional statements are used to determine employee bonuses based on their performance.
Bonus rules:
for(i in 1:length(name)){
if(performance[i] == "Very Good"){
bonus <- salary[i] * 0.20
}
else if(performance[i] == "Good"){
bonus <- salary[i] * 0.10
}
else{
bonus <- salary[i] * 0.05
}
cat("Name:", name[i], ", Bonus:", bonus,"\n")
}
Name: Bagas , Bonus: 500
Name: Joan , Bonus: 350
Name: Alya , Bonus: 325
Name: Dwi , Bonus: 1000
Name: Nabil , Bonus: 600
result_name <- c()
result_bonus <- c()
for(i in 1:length(name)){
if(performance[i] == "Very Good"){
bonus <- salary[i] * 0.20
}
else if(performance[i] == "Good"){
bonus <- salary[i] * 0.10
}
else{
bonus <- salary[i] * 0.05
}
result_name[i] <- name[i]
result_bonus[i] <- bonus
}
# membuat tabel hasil
result_table <- data.frame(
Name = result_name,
Bonus = result_bonus
)
knitr::kable(result_table)
| Name | Bonus |
|---|---|
| Bagas | 500 |
| Joan | 350 |
| Alya | 325 |
| Dwi | 1000 |
| Nabil | 600 |
Interpretation:
The program checks each employee’s performance using if, else if, and else. Based on the performance category, the program calculates the bonus according to the specified percentage.
In this example, the program displays employees who have a salary greater than 6000.
for(i in 1:length(name)){
if(salary[i] > 6000){
cat("Name:", name[i], ", Salary:", salary[i],"\n")
}
}
Name: Joan , Salary: 7000
Name: Alya , Salary: 6500
Name: Dwi , Salary: 10000
Name: Nabil , Salary: 12000
# filter salary > 6000
salary_filter <- employee_data[employee_data$Salary > 6000, c("Name","Salary")]
knitr::kable(salary_filter, row.names = FALSE)
| Name | Salary |
|---|---|
| Joan | 7000 |
| Alya | 6500 |
| Dwi | 10000 |
| Nabil | 12000 |
Interpretation:
The for loop iterates through each employee’s data. The program checks whether the salary is greater than 6000 and displays only those employees who meet the condition.
A while loop continues running as long as the condition remains true. In this example, the loop stops when an employee with the position Manager is found.
i <- 1
while(i <= length(name)){
cat("Name:", name[i], ", Position:", position[i],"\n")
if(position[i] == "Manager"){
cat("Stop here\n")
break
}
i <- i + 1
}
Name: Bagas , Position: Staff
Name: Joan , Position: Supervisor
Name: Alya , Position: Staff
Name: Dwi , Position: Manager
Stop here
i <- 1
result <- data.frame(Name=character(), Position=character())
while(i <= length(name)){
result <- rbind(result, data.frame(
Name = name[i],
Position = position[i]
))
if(position[i] == "Manager"){
break
}
i <- i + 1
}
knitr::kable(result)
| Name | Position |
|---|---|
| Bagas | Staff |
| Joan | Supervisor |
| Alya | Staff |
| Dwi | Manager |
Interpretation:
The program reads the employee data sequentially using a while loop. When the program finds an employee whose position is Manager, the loop stops using the break statement.
The break statement is used to stop a loop when a specific condition is met.
for(i in 1:length(name)){
if(salary[i] > 10000){
cat("Stopped because salary above 10000\n")
break
}
cat("Name:", name[i], ", Salary:", salary[i],"\n")
}
Name: Bagas , Salary: 5000
Name: Joan , Salary: 7000
Name: Alya , Salary: 6500
Name: Dwi , Salary: 10000
Stopped because salary above 10000
i <- 1
result_break <- data.frame(Name=character(), Salary=numeric())
for(i in 1:length(name)){
if(salary[i] > 10000){
break
}
result_break <- rbind(result_break, data.frame(
Name = name[i],
Salary = salary[i]
))
}
knitr::kable(result_break)
| Name | Salary |
|---|---|
| Bagas | 5000 |
| Joan | 7000 |
| Alya | 6500 |
| Dwi | 10000 |
Interpretation:
The program checks each employee’s salary. When it encounters a salary greater than 10000, the loop stops immediately using the break statement.
The continue statement is used to skip a specific iteration of a loop. In R, this is done using the next command.
for(i in 1:length(name)){
if(performance[i] == "Average"){
next
}
cat("Name:", name[i], ", Performance:", performance[i],"\n")
}
Name: Bagas , Performance: Good
Name: Joan , Performance: Very good
Name: Dwi , Performance: Good
Name: Nabil , Performance: Very good
cat("(Alya is skipped because the performance is 'Average')")
(Alya is skipped because the performance is 'Average')
result_continue <- data.frame(Name=character(), Performance=character())
for(i in 1:length(name)){
if(performance[i] == "Average"){
next
}
result_continue <- rbind(result_continue, data.frame(
Name = name[i],
Performance = performance[i]
))
}
knitr::kable(result_continue)
| Name | Performance |
|---|---|
| Bagas | Good |
| Joan | Very good |
| Dwi | Good |
| Nabil | Very good |
Interpretation:
If an employee has Average performance, the program skips that iteration using next. As a result, Alya’s data is not displayed in the output.
In this practicum, several fundamental programming concepts were practiced, including conditional statements and loops. Conditional statements help programs make decisions, while loops allow processes to repeat automatically on datasets. Additionally, break stops loops when a condition is met, and next skips specific iterations.
Siregar, B. (2025). Data Science Programming: Study Case Using R and Python. Online module. bookdown.org. Retrieved from https://bookdown.org/dsciencelabs/data_science_programming/02-Syntax-and-Control-Flow.html