The **for in range** in R is a syntax to iterate for loop with a range of values from starting and ending position. Someone asked me how to use `for i in range`

in R programming in comments, I would like to take some time to cover this with a few examples.

If you are not familiar with for loop in R, the **for loop** is used to repeatedly execute a set of statements or block of code for every element in a sequence or range. The for loop is always used with sequence objects like a list, vector, or array. Loop continues until we reach the last item in the sequence or until the break statement encounter.

## 1. Syntax of for in Range

Below is the syntax of **for in a range** of values. Here, the range is the range of values that contains start:stop.

```
# for in range syntax
for (var in range) {
# statement(s)
}
```

## 2. for in Range of Numeric Values in R

By using `for`

we can execute the block of code repeatedly for all elements in a range in R. The below example uses 5:10 range which ideally creates a vector with numeric values between 5 and 10 (including start and end values).

```
# for i in range example
numeric_range <- 5:10
for(i in numeric_range) {
print(paste("i value : ", i))
}
```

Yields below output. This example prints values from starting and ending to the console.

You can also write the above statement as follows (using the range within `for`

loop).

```
# for i in letters range example
for(i in 5:10) {
print(paste("i value : ", i))
}
```

Yields the same output as above.

## 3. for in Range of Letters

Similarly, you can also use `for`

with a range of letters. To get the range of letters you should use `LETTERS[start:stop]`

. Here, the start and stop would be numeric values and it returns the alphabet of the number position.

```
# for i in letters range example
for(i in LETTERS[2:5]) {
print(paste("i value : ", i))
}
```

Yields below output.

## Conclusion

In this article, you have learned how to use for i in range by R example. Also, learned `for`

with a range of letters. To get the range of letters you should use `LETTERS[start:stop]`

. The for in range in R is a syntax to iterate for loop with a range of values from starting to ending position.

## Related Articles

- Looping in R (for, while, repeat) With Examples
- For Loop in R With Examples
- Repeat Loop in R
- While Loop in R – With Examples
- Nested For Loop in R
- Break and Next (Continue) Statements in R
- R if, if…else, if…else…if Usage with Examples
- R if…else with Multiple Conditions
- R ifelse() Function