x <- 5
if (x < 1) {
y <- 0
} else if (x < 3) {
y <- 0.5
} else {
y <- 1
}
cat("Value of y is:",y,"\n")
y <- if (x < 1){
1
} else {
0
}
cat("Value of y is:",y,"\n")
x<- c('a','b','c','d')
cat("First type of for loop:\n")
for (i in 1:4) {
print(x[i])
}
cat("Second type of for loop:\n")
for (i in seq_along(x)){
print(x[i])
}
cat("Third type of for loop:\n")
for (l in x) {
print(l)
}
M <- matrix(1:6,2,3)
for (i in 1:nrow(M)) {
for (j in 1:ncol(M)) {
cat("M(",i,",",j,")=",M[i,j]," ")
}
cat("\n")
}
count <- 0
y <- 0
while (count < 10) {
count <- count + 1
if (count == 7) {
next
}
y <- c(y, count^2)
}
print(y)