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

R Programming Control Statement Notes

The document provides an overview of control statements in R programming, which are essential for controlling program flow based on conditions. It details various types of control structures, including if, else, for loops, while loops, break and next statements, repeat loops, switch statements, nested loops, and return statements, along with their syntax and examples. These control statements allow for decision-making, iteration, and managing the execution of code in R.
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)
3 views

R Programming Control Statement Notes

The document provides an overview of control statements in R programming, which are essential for controlling program flow based on conditions. It details various types of control structures, including if, else, for loops, while loops, break and next statements, repeat loops, switch statements, nested loops, and return statements, along with their syntax and examples. These control statements allow for decision-making, iteration, and managing the execution of code in R.
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
You are on page 1/ 18

Control statements in R

Control Statement: Control statements are expressions used to control the


execution and flow of the program based on the conditions provided in the
statements. These structure are used to make a decision after assessing the
variable.
Control structures are an essential aspect of any programming language
including R, They allow to control the flow of program by making decisions,
repeating action and defining.
These are the following types of control statements
1)if condition
2)else condition
3) else if condition
4) for loop
5) while loop
6) Nested loop
7)Repeat & break statement
8)Return statement
9)Next statement
10)switch
********************************************************
1)if condition: It is one of the control statements in R programming that
consist of a Boolean expression and a set of statements. If the Boolean
expression evaluates to TRUE, the set of statement is executed. Or If the
Boolean expression evaluates to FALSE, the set of statements after the end of
the If statement are executed.
syntax: if(condition){
statementsX
}
statementY
Example-1
a<-15
b<-20
if(b<a){
print("b is greater than a")
}
Example-2
X<-24L
Y<-“Reena”
If(is.integer(X)
{
Print(“X is an Integer”)
}

2) Else statement: Else statement are similar to if condition but when the test
expression in if condition fails than statements in else condition are executed.
Therefore in the if-else statement, an if statement is followed by an else
statement, which contain a block of code to be executed when the Boolean
expression in the if statement evaluate to FALSE.
EXAMPLE
x<-100
if(x>105){
print(paste(x, "is greater than 105"))
}else{
print(paste(x, "is not greater than 105"))
}

3) else if condition: An else if statement is include between if and else


statements. 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.
Example-1
x<-105
if(x>101){
print(paste(x,"is greater than 101"))
}else if (x<101){
print(paste(x,"is less than 101"))
}else{
print(paste(x,"is equal to 101"))
}
Example-2
x<-100
if(x>101){
print(paste(x,"is greater than 101"))
}else if (x<101){
print(paste(x,"is less than 101"))
}else{
print(paste(x,"is not equal to 101"))
}
Example-3

x<-101
if(x>101){
print(paste(x,"is greater than 101"))
}else if (x<101){
print(paste(x,"is less than 101"))
}else{
print(paste(x,"is equal to 101"))
}
4) For loop: For loop is a type of a loop or sequence of statements executed
repeatedly until exist condition is reached. For Loop 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.

syntax
for(value in sequence)
{
statement
}
EXAMPLE-1
x<-letters[5:10]
for(count in x){
print(count)
}
EXAMPLE-2
for(lu in seq(1,10)){
print(lu)
}
EXAMPLE-3
for(i in 1:5){
print(i^2)
}
EXAMPLE-4
z<-seq(1,5)
for(i in z){
print(i^2)
}
EXAMPLE-5
z<-c(1:10)
for(i in z){
print(i)
}
EXAMPLE-6
week<-c("sunday","monday","tuesday","wednesday")
for(A in week){
print(A)
}
5) While loop: The while loop is used to repeatedly execute a block of code as
long as a specified condition remains TRUE. While loop 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.
syntax: Initialization
while condition{
statement
updation
}
EXAMPLE-1
i=0
while(i<5){
print(i)
i=i+2

}
EXAMPLE-2
a<-2
while(a<41){
print(a)
a<-a+2
}

EXAMPLE-3
loan=30000
while(loan>=0)
{
print(loan)
loan=loan-2000
}
6)BREAK STATEMETS: Break statements is used to exist a loop
prematurely. The break Statement in R is a jump statement that is used to
terminate the loop at a particular iteration.
EXAMPLE-1
i=1
n=10
while(i<=n)
{
if(i==6)
break
print(i)
i=i+1
}
EXAMPLE-2
x<-101:108
for(val in x){
if(val==106){
break
}
print(val)
}
EXAMPLE-3
loan=30000
while(loan>=0)
{
if(loan==15000)
break
print(loan)
loan=loan-1000
}
7) Next statement: Next statement is used to skip the current iteration and
continue with the next iteration cycle without terminating the loop.

EXAMPLE-1
i=0
n=10
while(i<=n)
{
i=i+1
if(i==6)
next
print(i)
}

EXAMPLE-2
for(i in 101:110){
if(i==108)
next
print(i)
}
Notes: The difference between the break and Next statement in R is to one
(break) terminate the loop and another one (Next) is to skip the current
iterations. And both Break and Next statement are use with for loop only.
8) Repeat loop : The repeat loop is used to create an infinite loop. It is often
combined with the break statement to exist the loop when specific condition is
met.
Repeat is a loop which can be iterated many numbers of times but there is no
exist condition to come out from the loop. Therefore, break statement is used
to exist from the loop. Break statement can be used in any type of loop to exist
from the loop.
syntax: repeat
{
statements
break
}
EXAMPLE-1
i=1
repeat
{
if(i==10)
break
print(i)
i=i+1
}
EXAMPLE-2
v<-c("Hello Team")
x<-1
repeat{
print(v)
x<-x+1
if(x>=6){
break
}
}
EXAMPLE-3
emi=3000
repeat
{
if(emi==1500)
break
print(emi)
emi=emi-500
}

9)Switch function: The switch statement is used to select and execute one of
several blocks of code based on the value of an expression.
Switch can be implemented in two ways
1) Based on index value
2)Based on matching value
1) Based on index value
synatx:switch(expr,case1,case2,casen--)
Note: In the above syntax expression is input and based upon the input the
value will be return. Switch will be return the corresponding element of the
value.
EXAMPLE-1
x=switch(1,112,145,178,200,234,289)
print(x)
EXAMPLE-2
z<-switch(4,
"AGE",
"GEN",
"RACE",
"HEIGHT",
"WEIGHT")
Print(z)
2) Based on Matching value
EXAMPLE-1
day<-"Friday"
message<-switch(day,
"monday"="Its the start of the workweek",
"Tuesday"="Anothe work day",
"Wednesday"="Midweek!",
"Thursady"="Almost there!",
"Friday"="TGIF",
"Saturday"="Weekend!",
"Sunday"="Weekend"
)
print(message)

10) Nested loop: Nested loops are similar to simple loops. Nested means loop
inside the loop.
EXAMPLE-1
for(i in 1:3){
for(j in 1:2){
print(i+j)
}
}
Note: i=1 2 3
j=1 2
#case1: 1+1
#case2: 1+2
#case3: 2+1
#case4: 2+2
#case5: 3+1
#case6: 3+2

EXAMPLE-2
t=5
for(count in 2:10)
{
cat(t,"x",count,"=",t*count,"\n")
}

rows=6
for(i in 1:rows){
for(j in 1:i){
cat("Time-zone,")
}
cat("\n")
}
EXAMPLE-3
rows=6
for(i in 1:rows){
for(j in 1:i){
cat("*")
}
cat("\n")
}

11) Return statement: Return statement is used to return the result of an


executed function and return control to the calling function.
EXAMPLE-1
large<-function(a,b){
if(a>b)
return(a)
else
return(b)
}
c<-large(46,78)
print(c)
EXAMPLE-2
func<-function(x){
if(x>0){
return("positive")
}else if(x<0){
return("Negative")
}else{
return("Zero")
}
}

func(56)
func(0)
func(-6)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy