# R Loops – Repeat Loop, While Loop, FOR Loop

Sharing is Caring

Loops (R Loops) are used to repeat execution of a block of statements based on some predefined conditions. In R Programming we have following types of loops that can be used as per development requirement.

## Repeat Loop in R Programming

Repeat loop in R executes the set of statements until the defined condition is satisfied

```#Print some value using Repeat Loop

count <- 1

repeat {
print("R Programming")
count <- count+1

if(count > 5) {
break
}
}```

and output is

```[1] "R Programming"
[1] "R Programming"
[1] "R Programming"
[1] "R Programming"
[1] "R Programming"```

#### Another Example

```#Print table of 10 using Repeat Loop
a <- 10
count <- 1

repeat {
print(a*count)
count <- count+1

if(count > 10) {
break
}
}```

and output is

```[1] 10
[1] 20
[1] 30
[1] 40
[1] 50
[1] 60
[1] 70
[1] 80
[1] 90
[1] 100```

## While Loop in R Programming

While loop checks the condition before starting loop execution and executes the set of statements while the defined condition is true.

```#Declare and Initialise a varable
a <- 1

#Check condition in a while loop
while(a<=5){
print(a)
a <- a+1
}```

and output is

```[1] 1
[1] 2
[1] 3
[1] 4
[1] 5```

## FOR Loop in R Programming

When you need to execute a code to the specific number of time then FOR loop comes into action. Here we’ll see that how to iterate a vector using FOR loop.

```#Declare and Initialise a Vector
a <- c(10,TRUE,"Ashish",2.4)

#Iterate vector using FOR loop
for(i in a){
print(i)
}```

and output is

```[1] "10"
[1] "TRUE"
[1] "Ashish"
[1] "2.4"```

Cheers 🙂 Happy Learning