You are on page 1of 3

LOOPS:

Statements are executed sequentially, as first come first in basis.

For-loop-
It is a repetitive control.
Syntax-
For(value in vector) {
Example:
v<- LETTERS[1:4]
for(i in v) {
print(i)
}
Op:
[1] "A"
[1] "B"
[1] "C"
[1] "D"

Example 2: LETTERS[1:26]
OP:
[1] "A"
[1] "B"
[1] "C"
[1] "D"
[1] "E"
[1] "F"
[1] "G"
[1] "H"
[1] "I"
[1] "J"
[1] "K"
[1] "L"
[1] "M"
[1] "N"
[1] "O"
[1] "P"
[1] "Q"
[1] "R"
[1] "S"
[1] "T"
[1] "U"
[1] "V"
[1] "W"
[1] "X"
[1] "Y"
[1] "Z"
While loop:
v<- c ("Hello", "while loop")
cnt<- 2
while(cnt<7) {
print(v)
cnt=cnt+1
}
Op :
[1] "Hello" "while loop"
[1] "Hello" "while loop"
[1] "Hello" "while loop"
[1] "Hello" "while loop"
[1] "Hello" "while loop"

>

Repeat loop:
It will execute the code again and again until it met the stop condition.
v<- c("Hello","repeat loop")
cnt<-2
repeat {
print(v)
cnt<-cnt+1
if(cnt>5) {
break
}
}
OP:
[1] "Hello" "repeat loop"
[1] "Hello" "repeat loop"
[1] "Hello" "repeat loop"
[1] "Hello" "repeat loop"
Functions:
A function is a set of statements organised together to perform a specific task.
Built in functions- seq(), mean(), max(), min(), sum(), paste(), etc.
User defined functions-
Example:

You might also like