Decision Making
Decision making is a prime feature of any programming language. It allows us to make a decision, based on the result of a condition. Decision making is involved in order to change the sequence of the execution of statements, depending upon certain conditions.
A set of statements is provided to the program, along with a condition. Statements get executed only if the condition stands true, and, optionally, an alternate set of statements is executed if the condition becomes false.
So now, that we have read what is decision making, let’s have a quick glance at the topics which will be covered in this tutorial:
R provides the following decision-making statements:
If Statement
It is one of the control statements in R programming that consists of a Boolean expression and a set of statements. If the Boolean expression evaluates to TRUE, the set of statements is executed. If the Boolean expression evaluates to FALSE, the statements after the end of the If statement are executed.
The basic syntax for the If statement is given below:
if(Boolean_expression) {
This block of code will execute if the Boolean expression returns TRUE.
}
For example:
x <- “Intellipaat”
if(is.character(x)) {
print("X is a Character")
}
Output:[1] “X is a Character”
Get familiar with the top R Interview Questions to get a head start in your career!
Else Statement
In the If -Else statement, an If statement is followed by an Else statement, which contains a block of code to be executed when the Boolean expression in the If the statement evaluates to FALSE.
The basic syntax of it is given below:
if(Boolean_expression) {
This block of code executes if the Boolean expression returns TRUE.
} else {
This block of code executes if the Boolean expression returns FALSE.
}
For example:
x <- c("Intellipaat","R","Tutorial")
if("Intellipaat" %in% x) {
print("Intellipaat")
} else {
print("Not found")
}
Output: [1] “Intellipaat”
Else If Statement
An Else if statement is included between If and Else statements. Multiple Else-If statements can be included after an If statement. Once an If a statement or an Else if statement evaluates to TRUE, none of the remaining else if or Else statement will be evaluated.
The basic syntax of it is given below:
if(Boolean_expression1) {
This block of code executes if the Boolean expression 1 returns TRUE
} else if(Boolean_expression2) {
This block of code executes if the Boolean expression 2 returns TRUE
} else if(Boolean_expression3) {
This block of code executes if the Boolean expression returns TRUE
} else {
This block of code executes if none of the Boolean expression returns TRUE
}
For example:
x <- c("Intellipaat","R","Tutorial")
if("Intellipaat" %in% x) {
print("Intellipaat")
} else if ("Tutorial" %in% x)
print("Tutorial")
} else {
print("Not found")}
Output:[1] “Intellipaat”
Switch Statement
The switch statement is one of the control statements in R programming which is used to equate a variable against a set of values. Each value is called a case.
The basic syntax for a switch statement is as follows:
switch(expression, case1, case2, case3....)
For example:
x <- switch(
3,
"Intellipaat",
"R",
"Tutorial",
"Beginners"
)
print(x)
Output:[1] “Tutorial”
If the value passed as an expression is not a character string, then it is coerced to an integer and is compared with the indexes of cases provided in the switch statement.
y <- "12"
x <- switch(
y,
"9"= "Good Morning",
"12"= "Good Afternoon",
"18"= "Good Evening",
"21"= "Good Night"
)
print(x)
Output:[1] “Good Afternoon”
If an expression evaluates a character string, then it is matched (exactly) to the names of the cases mentioned in the switch statement.
-
-
- If there is more than one match, the first matching element is returned.
- No default argument is available.
Want to get certified in R? Learn R from top R experts and excel in your career with Intellipaat’s R Programming course!
Loops
The function of a looping statement is to execute a block of code, several times and to provide various control structures that allow for more complicated execution paths than a usual sequential execution.
The types of loops in R are as follows:
Repeat Loop
A repeat loop is one of the control statements in R programming that executes a set of statements in a loop until the exit condition specified in the loop, evaluates to TRUE.
The basic syntax for a repeat loop is given below:
repeat {
statements
if(exit_condition) {
break
}
}
For example:
v <- 9
repeat {
print(v)
v=v-1
if(v < 1) {
break
}
}
Output:
[1] 9
[1] 8
[1] 7
[1] 6
[1] 5
[1] 4
[1] 3
[1] 2
[1] 1
If we don’t place a break condition in the repeat loop statement, the statements in the repeat block will get executed in an infinite loop.
While Loop
A while loop is one of the control statements in R programming which executes a set of statements in a loop until the condition (the Boolean expression) evaluates to TRUE.
The basic syntax of a while loop is given below
while (Boolean_expression) {
statement
}
For example:
v <-9
while(v>5){
print(v)
v = v-1
}
Output:
[1] 9
[1] 8
[1] 7
[1] 6
For Loop
For loop is one of the control statements in R programming that executes a set of statements in a loop for a specific number of times, as per the vector provided to it.
The basic syntax of a for loop is given below
for (value in vector) {
statements
}
For example:
v <- c(1:5)
for (i in v) {
print(i)
}
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
We can also use the break statement inside a for-loop to break it out abruptly.
For example:
v <- c(1:5)
for (i in v) {
if(i == 3){
break
}
print(i)
}
Output:[1] 1
[1] 2
To learn more about R, go through our R programming Tutorial!
Loop-control Statements
Loop-control statements are part of control statements in R programming that are used to change the execution of a loop from its normal execution sequence.
There are two loop-control statements in R
Break Statement
A break statement is used for two purposes
-
-
- To terminate a loop immediately and resume at the next statement following the loop.
- To terminate a case in a switch statement.
For example:
v <- c(0:6)
for (i in v) {
if(i == 3){
break
}
print(i)
}
Output:
[1] 0
[1] 1
[1] 2
Interested in learning R Programming? Click here to learn more in this R Programming Training in Hyderabad!
Next Statement
A next statement is one of the control statements in R programming that is used to skip the current iteration of a loop without terminating the loop. Whenever a next statement is encountered, further evaluation of the code is skipped and the next iteration of the loop starts.
For example:
v <- c(0:6)
for (i in v) {
if(i == 3){
next
}
print(i)
}
Output:
[1] 0
[1] 1
[1] 2
[1] 4
[1] 5
[1] 6
In this tutorial, we learned what control statements in R programming are, what decision-making is, different decision-making statements in R, and how to use these statements to change the order of execution of a program. We also covered loops, different types of loops, and loop-control statements. In the next session, we are going to talk about the functions and types of functions in R. Let’s meet there!