Syntax and Control Flow

Practicum ~ Week 4

ADAM RICHIE WIJAYA

Data Science Student

Institut Teknologi Sains Bandung (ITSB)

Data Scientist & Analyst

📊 Regression Analysis

📈 Data Visualization

💻 R & Python

1 ). Introduction

Efficiency in compensation management is now achievable through R-based data analysis. By integrating iterative logic and visualization tools, we can transform raw employee datasets into transparent, accurate, and easily digestible bonus information for stakeholders.

2 ). employee dataset

nama <- c("Bagas","Joan","Alya","Dwi","Nabil")
usia <- c(25,30,27,35,40)
gaji <- c(5000,7000,6500,10000,12000)
posisi <- c("Staff","Supervisor","Staff","Manager","Director")
kinerja <- c("Good","Very Good","Average","Good","Very Good")

semua_bonus <- c()

for(i in 1:length(nama)){
  
  if(kinerja[i] == "Very Good"){
    bonus_temp <- gaji[i]*0.20
  } else if(kinerja[i] == "Good"){
    bonus_temp <- gaji[i]*0.10
  } else{
    bonus_temp <- gaji[i]*0.05
  }
  
  semua_bonus[i] <- bonus_temp
  
  cat("Name:",nama[i],", Bonus:",bonus_temp,"\n")
}
## Name: Bagas , Bonus: 500 
## Name: Joan , Bonus: 1400 
## Name: Alya , Bonus: 325 
## Name: Dwi , Bonus: 1000 
## Name: Nabil , Bonus: 2400

2.1 Performance Impact on Bonuses

The visualization highlights that performance is a key driver in increasing total earnings beyond the base salary:

  • “Very Good” Performance Effect: Joan and Nabil receive the largest bonus proportions (20%). This is visually evident by the taller orange segments in their respective bars.

  • Performance vs. Position: Even though Dwi has a high base salary as a Manager, his bonus percentage is lower than Joan’s due to the difference in performance ratings (Good vs. Very Good). This suggests the company’s incentive system effectively rewards individual merit over just seniority.

3 ). For loop (salary > 6000)

for(i in 1:length(nama)){
  
  if(gaji[i] > 6000){
    cat("Name:",nama[i],", Salary:",gaji[i],"\n")
  }
}
## Name: Joan , Salary: 7000 
## Name: Alya , Salary: 6500 
## Name: Dwi , Salary: 10000 
## Name: Nabil , Salary: 12000

3.1 Summary Key Metrics:

  • Highest Salary: 12,000 (Nabil)

  • Lowest Salary: 5,000 (Bagas)

  • Median Salary: 7,000

4 ). While loop until manager

i <- 1

while(i <= length(nama)){
  
  cat("Name:",nama[i],", Position:",posisi[i],"\n")
  
  if(posisi[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)

4.1 Summary:

  • Processed Items: 4 (Bagas, Joan, Alya, Dwi)

  • Excluded Items: 1 (Nabil)

  • Termination Trigger: Posisi == “Manager”

5 ). Break if salary > 10000

for(i in 1:length(nama)){
  
  if(gaji[i] > 10000){
    cat("(Stopped because",nama[i],"has a salary above 10,000)\n")
    break
  }
  
  cat("Name:",nama[i],", Salary:",gaji[i],"\n")
}
## Name: Bagas , Salary: 5000 
## Name: Joan , Salary: 7000 
## Name: Alya , Salary: 6500 
## Name: Dwi , Salary: 10000 
## (Stopped because Nabil has a salary above 10,000)

5.1 Interpretation

The visualization demonstrates a conditional termination logic where the loop processes the dataset sequentially—evaluating Bagas, Joan, Alya, and Dwi—until the specific threshold condition of a salary exceeding 10,000 is met at the final entry. As illustrated in , the process successfully executes for the first four employees, but triggers an immediate break upon encountering Nabil (12,000), effectively halting the iteration before his data is processed. This control flow structure highlights a proactive constraint where the operation terminates automatically once an organizational salary ceiling is reached, ensuring that subsequent data points exceeding this defined limit remain outside the execution scope.

6 ). Continue (Skip average)

for(i in 1:length(nama)){
  
  if(kinerja[i] == "Average"){
    cat("(",nama[i],"is skipped because performance is Average )\n")
    next
  }
  
  cat("Name:",nama[i],", Performance:",kinerja[i],"\n")
}
## Name: Bagas , Performance: Good 
## Name: Joan , Performance: Very Good 
## ( Alya is skipped because performance is Average )
## Name: Dwi , Performance: Good 
## Name: Nabil , Performance: Very Good

6.1 Interpretation

The visualization illustrates a conditional loop flow where the program evaluates each employee’s performance level; as shown in the plot, the next command effectively filters out records tagged as “Average” (represented by red markers), allowing the script to bypass these entries and continue iterating through the remaining “Processed” data (represented by green markers) without halting the entire execution. This control flow structure highlights a targeted data processing approach, where specific subsets are intentionally excluded from the final output while the overall loop cycle remains fully operational to process all valid entries.

7 Referensi

  • Computer Science 101 (Harvard’s CS50): Available on edX or YouTube. This is the global gold standard for understanding how algorithms and control flow work under the hood.

  • “Think Like a Programmer” by V. Anton Spraul: An excellent book that focuses on how to break down complex problems into logical code structures, rather than just memorizing syntax.

  • Algorithmic Thinking: Study pseudocode. Writing out logic in plain language (as we did in our previous chats) before writing actual code is the secret to becoming a proficient developer.