Control structures are a fundamental part of any programming language, including R. They help us control the flow of our programs. In simpler terms, control structures are like traffic lights for a program, directing it on when to stop, go, or take different paths. In this tutorial, we’ll introduce you to the most common control structures in R: conditional statements and loops.
1. Conditional Statements (if-else)
Conditional statements are used when we want to make decisions in our program. For example, “If it’s raining, bring an umbrella; otherwise, don’t.” In R, we use the if
and else
statements to do this.
Basic if
Statement
This statement checks if a condition is true. If it is true, the program runs some code; if it is not, it does nothing.
x <- 5
if (x > 3) {
print("x is greater than 3")
}
In this example:
x
is 5.- The condition
x > 3
is true because 5 is indeed greater than 3. - So, the program prints “x is greater than 3”.
if-else
Statement
Sometimes, we want to do something if a condition is true, and something else if it is false. For that, we use the else
part.
x <- 2
if (x > 3) {
print("x is greater than 3")
} else {
print("x is less than or equal to 3")
}
Here:
x
is 2.- The condition
x > 3
is false. - So, the program will print “x is less than or equal to 3”.
else if
Statement
What if we have more than two options? We can use else if
to check multiple conditions.
x <- 8
if (x > 10) {
print("x is greater than 10")
} else if (x > 5) {
print("x is greater than 5 but less than or equal to 10")
} else {
print("x is less than or equal to 5")
}
In this case:
x
is 8.- The first condition (
x > 10
) is false. - The second condition (
x > 5
) is true, so the program prints “x is greater than 5 but less than or equal to 10”.
2. Loops
Loops are used to repeat actions multiple times. They save us from writing the same code over and over.
for
Loop
A for
loop repeats a block of code a set number of times. It is useful when you know how many times you want to repeat something.
for (i in 1:5) {
print(paste("The value of i is", i))
}
This loop:
- Starts at 1 and ends at 5.
- On each iteration, it prints the current value of
i
. So the output will be:The value of i is 1 The value of i is 2 The value of i is 3 The value of i is 4 The value of i is 5
while
Loop
A while
loop repeats a block of code as long as a condition is true. It’s useful when you don’t know how many times you need to repeat, but you do know the condition that stops it.
x <- 1
while (x <= 5) {
print(paste("The value of x is", x))
x <- x + 1
}
Here:
- The loop will keep running as long as
x
is less than or equal to 5. - The value of
x
starts at 1 and increases by 1 with each loop. - The output will be:
The value of x is 1 The value of x is 2 The value of x is 3 The value of x is 4 The value of x is 5
repeat
Loop
The repeat
loop is similar to the while
loop, but it doesn’t automatically check the condition at the beginning. You have to tell it when to stop using a break
statement.
x <- 1
repeat {
print(paste("The value of x is", x))
x <- x + 1
if (x > 5) {
break
}
}
In this example:
- The loop runs indefinitely until
x
becomes greater than 5. - When
x
becomes greater than 5, thebreak
statement stops the loop.
3. Control Flow Keywords
Sometimes, we want to stop a loop or skip certain parts of it. R provides two special keywords for this: break
and next
.
break
The break
keyword is used to stop a loop early.
for (i in 1:10) {
if (i == 5) {
break
}
print(i)
}
In this case:
- The loop will print numbers from 1 to 4.
- When
i
becomes 5, thebreak
statement will stop the loop.
next
The next
keyword is used to skip the current iteration of a loop and continue with the next one.
for (i in 1:5) {
if (i == 3) {
next
}
print(i)
}
Here:
- The loop will print 1, 2, 4, and 5.
- When
i
is 3, thenext
statement skips that iteration and moves to the next number.