0% found this document useful (0 votes)
2 views9 pages

Control Flow Statement

Computer

Uploaded by

Lakshmipriya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Control Flow Statement

Computer

Uploaded by

Lakshmipriya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Control Flow Statements in R programming

Introduction:
Control Flow Statements are expressions used to control the execution and flow of the
program based on the conditions provided in the statements. These structures are used to make a
decision after assessing the variable. In R programming, there are 8 types of control statements as
follows:

1. if condition
2. else condition
3. else-if condition
4. for loop
5. while loop
6. repeat and break statement
7. return statement
8. next statement
R- if condition:
The if statement is one of the easiest decision-making statements. It is used to decide whether
a certain statement or block of statements will be executed or not i.e. if a certain condition is true
then a block of statement is executed otherwise not.

Syntax if (expression) {
#statement to execute if condition is true
}

 If the expression is true, the statement gets


executed.
 But if the expression is FALSE, nothing
happens.
 The expression can be a logical/numerical
vector, but only the first element is taken into
consideration.
 In the case of numeric vector, zero is taken as
FALSE, rest as TRUE.
Fig.1 Flowchart for if statement

Example:
1. a<-5
if (a>0)
{
print("Positive number")
}

Output – “Positive number”

R- else condition:

The if-statement in Programming Language alone tells us that if a condition is true, it will
execute a block of statements and if the condition is false, it won’t. But what if we want to do
something else if the condition is false? Here comes the R Programming Language else statement. We
can use the else statement with the if statement to execute a block of code when the condition is false.

if (condition)
{
Syntax # code to be executed if condition is TRUE
} else
{
# code to be executed if condition is FALSE
}

 Control falls into the if block.


 The flow jumps to Condition.
 Condition is tested.
 If the Condition yields true, the body
inside if gets executed.
 If the Condition yields false, the body
inside else gets executed.
 Flow exits the if-else block.

Fig.2 Flowchart for else statement

Example:
1. a<-5
if (a<0) {
print ("Positive number")
} else {
print ("Negative number")
}

Output – “Negative number”

R- else if condition:
if-else-if ladder in R Programming Language is used to perform decision making. This
ladder is used to raise multiple conditions to evaluate the expressions and take an output based on
it. This can be used to evaluate expressions based on single or multiple conditions connected by
comparison or arithmetic operators. It is particularly useful to check a list of conditions within a
single loop.

if (outer-condition is true) {
Syntax
#execute this statement
} else if (inner-condition1 is true)
{
#execute this statement
}
.
.
.
else {
#execute this statement
}

There can be more than one else if statement in the ladder to check for a lot of conditions at
the same time, in this case, it works like a switch.

Example:
1. a<-0
if (a>0) {
print ("Positive number")
} else if (a<0) {
print ("Negative number")
} else {
print ("Entered number is zero")
}

Output – “Entered number is zero”

R – for loop

For loop in R programming language is useful to iterate over the elements of a list, data
frame, vector, matrix or any other object. It means the for loop can be used to execute a group of
statements repeatedly depending upon the number of elements in the object. It is an entry-
controlled loop, in this loop, the test condition is tested first, then the body of the loop is executed,
the loop body would not be executed if the test condition is false .

for (value in sequence)


Syntax {
statement
}

Fig. 3 for loop – flow diagram

Example:
1. a<- list (2, 5, "apple", 9.8)
for (i in a)
{
print (i)
}

Output
[1] 2
[1] 5
[1] “apple”
[1] 9.8

R- while loop:
While loop R programming language is used when the exact number of iterations of a loop
is not known beforehand. It executes the same code again and again until a stop condition is met.
While loop checks for the condition to be true or false n+1 times rather than n times. This is because
the while loop checks for the condition before entering the body of the loop.

while (test_expression){
statement
Syntax update_expression
}

 Control falls into the while loop.


 The flow jumps to Condition
 Condition is tested.
 If the Condition yields true, the flow goes into
the Body.
 If the Condition yields false, the flow goes
outside the loop

Fig. 4 while loop – flow diagram  The statements inside the body of the loop get
executed.
 Updation takes place.
 Control flows back .
 The while loop has ended and the flow has gone
Example: outside.
1. i<-5
while (i>0){
print (i)
i=i-1
}

Output
[1] 5
[1] 4
[1] 3
[1] 2
[1] 1

R- repeat loop and break statement:


repeat is a loop which can be iterated many number of times but there is no exit
condition to come out from the loop. So, break statement is used to exit from the loop. To terminate
the repeat loop, we use a jump statement that is the break keyword.

repeat {
Syntax statements

if (condition)
{
break
}
}

Example:
Fig. 5 repeat loop – flow diagram
1. b<-1
repeat {
print (b)
b<-b+1
if(b>5){
break
}
}

Output
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

R- return statement
return statement is used to return the result of an executed function and returns control to the
calling function.
Syntax return (expression)

Example:
1. func <- function(x){
if (x>0){
return("Positive")
}
}
func(1)

Output - “Positive”

R- next statement:

‘next’ is a loop control statement just like the break statement. But ‘next’ statement works
opposite to that of the break statement, instead of terminating the loop, it forces to execute the
next iteration of the loop. In R, the next statement is a control statement that allows you to jump to
the next iteration of a loop without running any of the statements that are still in the loop for the
current iteration. To process or skip specific cases or circumstances inside the loop, this phrase is
frequently used in conjunction with conditional statements (such as if statements).

Syntax next
Fig. 6 next – flow diagram

Example:
1. val <- 6:11
for (i in val)
{
if (i==8)
{
next
}
print (i)
}

Output
[1] 6
[1] 7
[1] 9
[1] 10
[1] 11

Conclusion:
In conclusion, understanding control flow in R is pivotal for effective programming.
Mastering conditionals (if-else), loops (for, while), and switch statements empowers efficient
workflow and logic implementation. By leveraging these constructs, developers can manage program
execution based on conditions, iterate over data structures, and handle multiple cases seamlessly.
Proficiency in R's control flow ensures code clarity, robustness, and scalability, essential for tackling
complex data analyses and statistical computations with precision and efficiency. Thus, embracing
control flow constructs elevates coding proficiency and enhances the ability to solve diverse analytical
challenges in the R programming environment.

References:
1. Control flow in R. (n.d.). [Link]
2. GeeksforGeeks. (2020, June 1). Control statements in R programming. GeeksforGeeks.
[Link]
3. GeeksforGeeks. (2024, May 26). R Programming Language Introduction. GeeksforGeeks.
[Link]
4. Sahu, B. K. (2024, March 22). Control flow statements in R - decision making and loops.
Intellipaat. [Link]
5. Sharma, A. (2020, January 30). Conditionals and control flow in R tutorial.
[Link]

You might also like