R Programming Control Statement Notes
R Programming Control Statement Notes
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"))
}
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")
}
func(56)
func(0)
func(-6)