Control Structures in R
Control structures are the statements which are used to control the execution and flow of the program based on the conditions.
R Programming supports the following control structures or statements. Those are,
- if
- if-else
- if-else-if
- nested if
- while
- for
- repeat and break
- next
if statement
The if statement is the simplest decision-making statement which helps us to take a decision based on the condition.
In other words, the if statement is used to test a specific condition. If the condition is true, a block of code (if-block) will be executed.
Syntax:
if (condition)
{
statements
}
Example:
Filename: ifdemo.R
x <- 50
# if statement
if(x > 10){
cat(x, "is greater than 10")
}
Output: $ Rscript ifdemo.R
50 is greater than 10
if-else statement
An if-else statement is the if statement followed by an else statement. In an if-else statement, the else block is executed when the condition is false. In simple words, if the condition is true then the if block gets executed, otherwise the else block gets executed.
Syntax:
if (condition){
statements
}else{
statements
}
Example:
Filename: ifelsedemo.R
num <- 21
# if-else statement
if(num %% 2 == 0){
print("Even Number")
}else {
print("Odd Number")
}
Output: $ Rscript ifelsedemo.R
[1] "Odd Number"
if-else-if statement
The if-else-if statement enables us to check multiple conditions and execute the specific block of statements depending upon the true condition among them.
Syntax:
if (condition1){
statements
}else if (condition2){
statements
}else if (condition3){
statements
}else {
statements
}
Example:
Filename: ifelseifdemo.R
age <- readline(prompt="Enter your age: ")
age <- as.integer(age)
if(age < 18) {
print("You are child")
} else if(age > 50) {
print("You are old guy")
} else {
print("You are adult")
}
Output:
Sample Run1:
> source("ifelseifdemo.R")
Enter your age: 36
[1] "You are adult"
Sample Run2:
> source("ifelseifdemo.R")
Enter your age: 16
[1] "You are child"
Note: readline() waits for the user's input only in an interactive R session (the R console or RStudio). When a script is run with Rscript, readline() does not wait and returns an empty string. So the programs that read input are run from the R console with source().
nested if statement
The nested-if is a conditional statement that contains another if statement within it. And it enables us to check multiple conditions.
Syntax:
if (condition1){
statements
if (condition2){
statements
}else{
statements
}
}else {
statements
}
Example:
Filename: nestedifdemo.R
num <- readline(prompt="Enter any number: ")
num <- as.integer(num)
if(num > 0) {
print("Number is Positive")
if(num %% 2 == 0) {
print("Number is Even")
} else {
print("Number is Odd")
}
} else {
print("Number is Negative")
}
Output:
Sample Run1:
> source("nestedifdemo.R")
Enter any number: 12
[1] "Number is Positive"
[1] "Number is Even"
Sample Run2:
> source("nestedifdemo.R")
Enter any number: -25
[1] "Number is Negative"
while loop statement
A while loop is a type of control flow statement which is used to iterate a block of code several times. The while loop terminates when the value of the condition becomes false.
Syntax:
while (condition) {
statement
}
Example:
Filename: whiledemo.R
# R program to print numbers from 1 to 5
n = 1
while(n <= 5) {
cat(n, " ")
n <- n + 1
}
Output: $ Rscript whiledemo.R
1 2 3 4 5
for loop statement
A for loop is the most popular control flow statement. A for loop is commonly used to iterate over items of a sequence (vector, list, ..).
Syntax:
for (value in sequence)
{
statement
}
Example:
Filename: fordemo.R
# R program to iterate the vector
names_vec <- c("Madhu", "Durga", "Naveen")
for(name in names_vec)
{
print(name)
}
Output: $ Rscript fordemo.R
[1] "Madhu"
[1] "Durga"
[1] "Naveen"
repeat and break statement
repeat is a simple loop which can be iterated many number of times but there is no exit condition to come out from the loop. So, the break statement is used to exit from the loop. The break statement can be used in any type of loop to exit from the loop.
Syntax:
repeat
{
statements
if (condition)
{
break
}
}
Example:
Filename: repeatdemo.R
# R program to demonstrate repeat and break
num <- 1
repeat {
cat(num, "\n")
num <- num + 1
if(num > 5) {
break
}
}
Output: $ Rscript repeatdemo.R
1
2
3
4
5
next statement
The next statement is used to skip the current iteration without executing the further statements and continues the next iteration cycle without terminating the loop.
Syntax:
next
Example:
Filename: nextdemo.R
# R program to demonstrate next
for(num in 1:10) {
if(num %% 2 == 0) {
next
}
cat(num, "\n")
}
Output: $ Rscript nextdemo.R
1
3
5
7
9