Conditional Statements and Loops
Conditional Statements and Loops
getwd()
setwd("D:/rstudio")
#conditional statements
#control/decision making structures: help in specifying one or more condition to be specified
#or evaluated along with a statement or statements to be executed under certain conditions
#or optionally execute another statement
#three elements/components of the syntex: 1)condition type 2)boolean expression or conditions
3)statements to be executed
#three types: IF statement/condition, IF-ELSE statement/condition, Switch case
#IF condition
#example1: check if the marks are equal to 100 or not
marks<-90
if(marks<100){print("marks re not equal to 100")}
#example2: check whether x is an integer
x<-25L
if(is.integer(x)){print("x is an integer")}
#example3: block of statements
p<-29
q<-98
if(p>q){print("p is greater than q")}
if(p<q){print("p is less than q")}
if(p==q){print("p is equal to q")}
#IF-ELSE condition: IF(condition){statement of IF block}ELSE{statement of ELSE block}
#example1 (check both commands for print option)
x1<-45
if(x1>40){print("x1 is greater than 40")}else{print("x1 is less than 40")}
if(x1>40){print(paste(x1," is greater than 40"))}else{print(paste(x1 ,"is less than 40"))}
#example2
y1<-9
if(y1==2){y1<-y1+1}else{y1<-2*y1}
print(y1)
p1<-11
if(p1==5){p1<-p1+2}else{p1<-3*p1}
print(p1)
#OR state condition of ifelse in one word or command:ifelse(condition as well as output command)
q1<-6
ifelse(q1==6, q1<-q1+1, q1<-2*q1)
#example3
r<--9
if(r>0){print("r is a positive number")}else if(r<0){print("r is a negative number")}else{print("r is a
zero")}
#switch case: executing a number of codes, each value called a case; suitable for qualitative or
nominal data
#syntex: switch(expression,"case1"=action1,"case2"=action2...)
#example 1 : switch case
grade<-"B"
switch(grade,"A"="excellent","B"="good","C"="average","D"="poor","E"="very poor")
grade1<-"E"
switch(grade1,"A"="excellent","B"="good","C"="average","D"="poor","E"="very poor")
#usage considerations- if the cases are mutually exclusive and we are dealing with multiple
cases/conditions
#default output: if the expression does not match with stated cases/options \n
#for numeric indexing- the last option is used as default, so state the default option at the end of
case
#for character switching- the last unspecified case is selected as default
#example 2: switch case (check by changing day number as 1 to 7 and next>7)
day_num<-5
day_name<-switch(day_num,"monday","tuesday","wednesday","thursday","friday","saturday","sun
day","NA")
print(day_name)
day_number<-9
day_name1<-switch(day_number,"monday","tuesday","wednesday","thursday","friday","saturday","
sunday","NA")
print(day_name1)
Loops
#loops: loop statement/ command allows to repeatedly execute a block of code.
#three types: FOR, WHILE and REPEAT
#the NEXT, BREAK, and REPEAT statements provide for additional control over loop execution
#such as- skipping iterations, exiting loop, or creating infinite loops until a condition is met
#FOR loop: iterates/repeats over a sequence (vector or list); the number of iterations is known as
advance.
#syntex:FOR(variable in n)[command to be executed] - n can be numeric, character, logical vector or
a list
#need to specify 1) a variable 2)a vector
#variable specified is set sequentially to all values in the vector
#and all commands are executed for all these values
#example1: print i^2 for all numbers in 2:7
for(i in 2:7){print(i^2)}
#example2: print days name
day_name<-c("monday","tuesday","wednesday","thursday","friday","saturday","sunday")
for(i in day_name){print(i)}
#nested loop: FOR loop for two dimensional or hierarchical data like MATRIX or LIST (2 dimensions - i
and j)
#example1: nested FOR loop- print every element of the matrix 3*4 dimension with data set 1:12
mat1<-matrix(1:12,3,4)
print(mat1)
for(i in 1:nrow(mat1)){for(j in 1:ncol(mat1)){print(mat1[i,j])}}
for(i in 1:ncol(mat1)){for(j in 1:nrow(mat1)){print(mat1[j,i])}}
#example2: nested FOR loop - create a multiplication table
mat2<-matrix(0:0,10,9)
print(mat2)
for(i in 1:nrow(mat2)){for(j in 1:ncol(mat2)){mat2[i,j]=i*j}}
print(mat2)
#WHILE loop: it continues to execute the same code again until a condition is met
#or remains TRUE or till it becomes FALSE after which iterations stop and the loop terminates
#number of iterations is not known in advance like the FOR loop
#syntex: while(condition){code to execute while the condition is true}
#example1: print y*2 for all numbers of y value between 120 to 126
y<-120
while(y<=126){print(y*2)
{y<-y+1}
}
#example2: print y1^2 for all values of y1 between 6 to 11
y1<-5
while (y1<=11) {print(y1^2)
{y1<-y1+1}
}
#repeat loop with break statement: a repeat statement creates infinite loop and requires a break
statement
#example1
x<-1
repeat{print(x)
x<-x+1
if(x>5){break}
}
#example2
i<-5
repeat{print(i)
i<-i+1
if(i>10){break}
}
#example3: print p^2 for all p between 4 to 10
p<-4
repeat{print(p^2)
p<-p+1
if(p>10){break}
}