Data Science Programming · Week 4

Prakticum Week-4

Data Science Program Study — Syntax and Control Flow

Wulan

Wulan Gustika A. T.

ID: 52250010

Dosen

Bakti Siregar, M.Sc., CDS

Lecturer

ITSB

Institut Teknologi Sains Bandung

University


1 Dataset

Learning Objectives

  • Understand and implement conditional statements (if, if-else, if-elif-else).
  • Apply loops (for loop, while loop, break, continue) to analyze a dataset.

Use the following dummy dataset:

Employee Dataset


2 Conditional Statement

Task — Bonus Calculation via Conditional Statement

Determine bonus levels based on employee performance:

Performance Bonus Rate
Very Good 20% of salary
Good 10% of salary
Average 5% of salary

Write a program in Python and R to calculate each employee’s bonus.

bonus <- c()   # create an empty vector to store the bonus values

for(i in seq_len(nrow(mydata))){   # loop through each row of the dataset
  
  if(mydata$Performance[i] == "Very Good")   # check if performance is "Very Good"
    {bonus[i] <- mydata$Salary[i] * 0.20}    # assign 20% of salary as bonus
  
  else if(mydata$Performance[i] == "Good")   # check if performance is "Good"
    {bonus[i] <- mydata$Salary[i] * 0.10}    # assign 10% of salary as bonus
  
  else{bonus[i] <- mydata$Salary[i] * 0.05}}  # otherwise assign 5% of salary as bonus

Result · Bonus per Employee


3 Loops (For & While)

3.1 For Loop

for loop

Task: Use a for loop to list employees with a salary greater than 6,000.

name   <- c()    # create an empty vector to store employee names
salary <- c()    # create an empty vector to store employee salaries

for(i in seq_len(nrow(mydata))){    # loop through each row of the dataset
  
  if(mydata$Salary[i] > 6000){     # check if the salary in the current row is greater than 6000
    
    name   <- c(name, mydata$Name[i])      # append the employee name to the name vector
    salary <- c(salary, mydata$Salary[i])}}  # append the employee salary to the salary vector

Result · Salary > 6,000

3.2 While Loop

while loop break

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

name     <- c()   # create an empty vector to store employee names
position <- c()   # create an empty vector to store employee positions
i        <- 1     # initialize the counter variable

while(i <= nrow(mydata)){   # repeat the loop while i is less than or equal to the number of rows
  
  name     <- c(name, mydata$Name[i])        # add the current employee's name to the name vector
  position <- c(position, mydata$Position[i])# add the current employee's position to the position vector
  
  if(mydata$Position[i] == "Manager"){ break } # stop the loop immediately if the position is "Manager"
  
  i <- i + 1}   # increment the counter to move to the next row

Result · Up to Manager Position

3.3 Break

break

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

name   <- c()    # create an empty vector to store employee names
salary <- c()    # create an empty vector to store employee salaries

for(i in seq_len(nrow(mydata))){   # loop through each row of the dataset
  
  if(mydata$Salary[i] > 10000){ break }   # stop the loop if the salary is greater than 10000
  
  name   <- c(name, mydata$Name[i])       # add the employee name to the name vector
  salary <- c(salary, mydata$Salary[i])}   # add the employee salary to the salary vector

Result · Loop Stops at Salary > 10,000

3.4 Continue

continue / next

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

name        <- c()   # create an empty vector to store employee names
performance <- c()   # create an empty vector to store performance values

for(i in seq_len(nrow(mydata))){   # loop through each row of the dataset
  
  if(mydata$Performance[i] == "Average"){ next }   # skip this iteration if performance is "Average"
  
  name        <- c(name, mydata$Name[i])           # add the employee name to the name vector
  performance <- c(performance, mydata$Performance[i])}  # add the performance value to the performance vector

Result · Excluding Average Performance


Assignment Week 4 · Data Science Programming · ITSB · March 2026