0% found this document useful (0 votes)
161 views

Control Structures in R

The document discusses control structures in R programming. It describes different types of control structures like if/else statements, for loops, while loops, and functions. If/else statements allow changing the execution sequence based on conditions. Loops like for and while loops execute blocks of code repeatedly. Functions allow organizing and reusing code to perform tasks. There are built-in and user-defined functions in R. Recursive functions call themselves to elegantly code solutions like calculating factorials.

Uploaded by

Disha Khurana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
161 views

Control Structures in R

The document discusses control structures in R programming. It describes different types of control structures like if/else statements, for loops, while loops, and functions. If/else statements allow changing the execution sequence based on conditions. Loops like for and while loops execute blocks of code repeatedly. Functions allow organizing and reusing code to perform tasks. There are built-in and user-defined functions in R. Recursive functions call themselves to elegantly code solutions like calculating factorials.

Uploaded by

Disha Khurana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Control Structures in R 

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.

In order to control the execution of the expressions flow in R, we make use of the control
structures. These control structures are also called as loops in R. There are eight types of
control structures in R:

 if
 if-else
 for
 nested loops
 while
 repeat and break
 next
 return
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.
}

x <- “Intellipaat”
if(is.character(x)) {  
print("X is a Character")
}

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.
}
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
}

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....)

x <- switch(  
3,  
"Intellipaat",  
"R",  
"Tutorial",  
"Beginners"
)
print(x)

Loopin Statement/iterative Statements


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. It works as a infinite loop.
The basic syntax for a repeat loop is given below:
repeat {
statements
if(exit_condition) {
break
}

Repeat

Num<- as.integer(readline(“Enter a number”))

If(num>0)

break;

cat(entered a right value”,num);


while Loops
While loops begin by testing a condition. If it is true, then they execute the loop body.
Once the loop body is executed, the condition is tested again, and so forth, until the
condition is false, after which the loop exits.

while (Boolean_expression) {
statement
}

v <-9
while(v>5){
print(v)
v = v-1
}

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
}

v <- c(1:5)
for (i in v) {
print(i)
}

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.
 v <- c(0:6)
 for (i in v) {
 if(i == 3){
 break
 }
 print(i)
 }

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:100)
for (i in v) {
if((i %% 5)==0){
next
}
print(i)
}
Functions in R
A function is a block of organized and reusable code that is used to perform a
specific task in a program. A function is created by using the function keyword.
The basic syntax of a function is given below:
FunctionName <- function(arg1, arg2, ...) {
Function Body
}

where,

 FunctionName is the name of a function that is stored as an R object.


 Arguments are used to provide specific inputs to a function while a function is
invoked. A function can have zero, single, multiple, or default arguments.
 Function Body contains the block of code that performs the specific task
assigned to a function.
 Return Value is the value that a function returns when it succeeds in performing
the task. We can also make a function with no return value.

R Functions Types

There are two types of functions in R:

 In-built functions
 User-defined functions

In-built Functions

These functions in R programming are provided by the R environment for direct


execution, to make our work easier.
Some examples for the frequently used in-built functions are as follows:
#seq() To create a sequence of numbers
print(seq(1,9))

#sum() To find the sum of numbers


print(sum(25,50))

User-defined Functions

These functions in R programming language are declared, and defined by a user


according to the requirements, to perform a specific task.
For example:
#Create a function to print the sum of squares of numbers in
sequence
sum = 0
Function1 <- function(x) {  
for(i in 1:x) {     
a <- i^2     
sum = sum + a     
print(sum)  
}
}         
#Calling a function
Function1(5)

Recursive Functions
Recursive means a function calling itself. The R Programming language introduced a
new technique called Recursion for elegant and straightforward coding.

n! = (n) * (n-1) * (n-2) * ….. * 1

It means, 6! = 6 * 5 * 4 * 3 * 2 * 1

We can achieve the same in R programming using For Loop, While Loop, etc. But if
you observe the above pattern, its behavior is repetitive, which means recursive. So
instead of writing the loops (costly), we can write the recursive functions R
Programming as it will utilize the memory in the form of stack. It will not contain
another space except the fixed size stack.

# Example

fact <- function(number)

if(number == 0 || number == 1) {

return (1)

} else {

return (number* fact(number - 1))

fact(6)

You might also like