If Loop

# if loop

#Example 1

team_A <- 3 # Number of goals scored by Team A
team_B <- 1 # Number of goals scored by Team B
if (team_A > team_B){
  print ("Team A wins")
}
## [1] "Team A wins"

In this example, you have two variables, team_A and team_B, representing the number of goals scored by two teams. The “if” statement checks if team_A has scored more goals than team_B. If the condition in the parentheses is true, it prints “Team A wins.” In this case, team_A (3 goals) is indeed greater than team_B (1 goal), so the message “Team A wins” is printed.

#Example 2

team_A <- 1 # Number of goals scored by Team A
team_B <- 3 # Number of goals scored by Team B
if (team_A > team_B){
    print ("Team A will make the playoffs")
}

In this example, the condition is the same, but the values of team_A and team_B are reversed. team_A has 1 goal, and team_B has 3 goals. The “if” statement still checks if team_A has scored more goals than `team_B,” but in this case, the condition is false, so the code block inside the “if” statement is not executed. Therefore, “Team A will make the playoffs” is not printed.

In summary, the “if” statement in these examples allows you to make decisions in your program based on conditions. If the condition is true, the code inside the “if” block is executed; otherwise, it is skipped.

if else loop

# if else loop

#Example 1

team_A <- 1 # Number of goals scored by Team A
team_B <- 3# Number of goals scored by Team B
if (team_A > team_B){
    print ("Team A will make the playoffs")
} else {
    print ("Team B will make the playoffs")
}
## [1] "Team B will make the playoffs"

In this example, you have two variables, team_A and team_B, representing the number of goals scored by two teams. The “if” statement checks if team_A has scored more goals than team_B. If the condition is true, it prints “Team A will make the playoffs.” If the condition is false (i.e., team_A is not greater than team_B), it prints “Team B will make the playoffs.”

# example 2

team_A <- 2 # Number of goals scored by Team A
team_B <- 2# Number of goals scored by Team B
if (team_A > team_B){
  print ("Team A won")
} else if (team_A < team_B){
  print ("Team B won")
} else {
  "Team A & B tied"
}
## [1] "Team A & B tied"

In this example, you still have team_A and team_B representing goals scored by two teams. The “if” statement checks if team_A has more goals than team_B. If true, it prints “Team A won.” If not, it proceeds to the “else if” block, which checks if team_A has fewer goals than team_B. If true, it prints “Team B won.” If neither of these conditions is met, it means that the scores are tied, and it prints “Team A & B tied.”

In summary, “if-else” and “if-else if-else” constructs are used to make decisions in your program based on conditions. The code inside the first “if” block that evaluates to true is executed, and if none of the conditions are true, the “else” block is executed.

for loop

# for loop

#example 1

teams <- c("team_A","team_B")
for (value in teams){
    print(value)
}
## [1] "team_A"
## [1] "team_B"

In this example, you have a vector teams containing two elements, “team_A” and “team_B.” The “for” loop iterates through each element in the teams vector, and for each iteration, it assigns the current element to the variable value. It then prints the value. So, in this case, it will print “team_A” and “team_B” in separate lines, one after the other.

# exaxmple 2

matches <- list(c(2,1),c(5,2),c(6,3))
total_goals <- c()
for (match in matches){
    total_goals <- c(total_goals, sum(match))
}

In this example, you have a list matches containing sublists, where each sublist represents the goals scored in different matches. You also initialize an empty vector total_goals. The “for” loop iterates through each match in the matches list. For each iteration, it calculates the sum of goals in the current match using the sum() function and appends that sum to the total_goals vector.

So, after the loop finishes, the total_goals vector will contain the total goals scored in each match, in the same order as they appear in the matches list.

In summary, “for loops” are used for iterating through elements in vectors, lists, or sequences. You can perform specific operations for each element during the loop

if else loop

# if else loop

matches <- list(c(2,1),c(5,2),c(6,3))
for (match in matches){
    if (match[1] > match[2]){
        print("Win")
    } else {
        print ("Lose")
    }
}
## [1] "Win"
## [1] "Win"
## [1] "Win"

In this R code, you have a list called matches containing sublists, where each sublist represents the goals scored in different matches. You’re using a “for loop” to iterate through each match and, for each match, you’re using an “if-else” statement to determine if the first element of the match sublist (which represents the goals scored by one team) is greater than the second element (which represents the goals scored by the opposing team). For each iteration of the loop, it checks whether the first element (match[1]) is greater than the second element (match[2]). If this condition is true, it prints “Win.” If the condition is false, it prints “Lose.”

So, based on the goal scores in each match, the loop will print “Win” when the first team scores more goals than the second team and “Lose” when the second team scores more or an equal number of goals as the first team.

for if else loop

# for if else loop

matches <- list(c(2,1),c(5,2),c(6,3))
for (match in matches){
    if (match[1] > match[2]){
        print("Win")
        break
    } else {
        print("Lose")
    }
}
## [1] "Win"

In this example this code will iterate through the matches one by one. When it encounters a match where the first team has scored more goals than the second team, it prints “Win” and then immediately exits the loop using the break statement. As a result, it will only evaluate the outcome of the first match in the list. If the first team doesn’t win, it will print “Lose.”

So, in this code, the loop will print “Win” for the first match (where the first team scores more) and then exit the loop.

while loop

# while loop

wins <- 0
while (wins < 10){
    print ("Does not make playoffs")
    wins <- wins + 1
}
## [1] "Does not make playoffs"
## [1] "Does not make playoffs"
## [1] "Does not make playoffs"
## [1] "Does not make playoffs"
## [1] "Does not make playoffs"
## [1] "Does not make playoffs"
## [1] "Does not make playoffs"
## [1] "Does not make playoffs"
## [1] "Does not make playoffs"
## [1] "Does not make playoffs"

In the provided code, a “while loop” is used to control the execution of a block of code. The loop’s purpose is to print the message “Does not make playoffs” repeatedly until a certain condition is met.

The loop begins with an initialization step, setting the variable wins to 0. This variable is used to keep track of the number of wins.

The while loop then checks a condition: whether the value of wins is less than 10. As long as this condition remains true, the code inside the loop is executed.

Within the loop, the program prints “Does not make playoffs” and then increments the wins variable by 1. This increment is crucial because it will eventually lead to the condition wins < 10 becoming false.

The loop repeats the process of printing the message and incrementing wins until wins reaches or exceeds 10. At this point, the condition becomes false, and the loop terminates.

As a result, the code will print “Does not make playoffs” 10 times, and then it will stop. This code simulates a situation where a team has not yet achieved the required number of wins to make the playoffs, and it keeps reminding you of that fact until the condition is met.

if else while loop

# if else while loop

# example 1

wins <- 0
while (wins <= 10){
    if (wins < 10){
        print("does not make playoffs")
    } else {
        print ("makes playoffs")
    }
    wins <- wins + 1
}
## [1] "does not make playoffs"
## [1] "does not make playoffs"
## [1] "does not make playoffs"
## [1] "does not make playoffs"
## [1] "does not make playoffs"
## [1] "does not make playoffs"
## [1] "does not make playoffs"
## [1] "does not make playoffs"
## [1] "does not make playoffs"
## [1] "does not make playoffs"
## [1] "makes playoffs"

In this example, you have a while loop that iterates as long as wins is less than or equal to 10. Inside the loop, there’s an if-else statement. If wins is less than 10, it prints “does not make playoffs.” When wins reaches 10 or greater, it prints “makes playoffs.” The loop increments wins by 1 in each iteration.

As a result, this code will print “does not make playoffs” 10 times, then it will print “makes playoffs” once when wins becomes 11.

# example 2

wins <- 0
playoffs <- c()
while (wins <= 15){
    if (wins < 10){
        print("does not make playoffs")
        playoffs <- c(playoffs, "does not make playoffs")
    } else {
        print ("makes playoffs")
        playoffs <- c(playoffs, "makes playoffs")
        break
    }
    wins <- wins + 1
}
## [1] "does not make playoffs"
## [1] "does not make playoffs"
## [1] "does not make playoffs"
## [1] "does not make playoffs"
## [1] "does not make playoffs"
## [1] "does not make playoffs"
## [1] "does not make playoffs"
## [1] "does not make playoffs"
## [1] "does not make playoffs"
## [1] "does not make playoffs"
## [1] "makes playoffs"

In this example, you have a while loop that operates as long as wins is less than or equal to 15. Within the loop, there’s an if-else statement similar to the first example. If wins is less than 10, it prints “does not make playoffs” and appends this information to the playoffs vector. Once wins becomes 10 or greater, it prints “makes playoffs,” appends this information to the playoffs vector, and then exits the loop using the break statement.

The outcome of this code is that it will print “does not make playoffs” 10 times, append this information to the playoffs vector 10 times, and then it will print “makes playoffs” once, appending it to the playoffs vector. So, the playoffs vector will contain a sequence of “does not make playoffs” followed by a single “makes playoffs.”

These examples demonstrate how you can use a while loop to simulate scenarios and control program flow based on certain conditions.