The R For Loop is used to repeat a block of statements until there are no items in the Vector. It is one of the most used ones in any programming language.
The basic syntax of the For loop in R Programming languages is
for(val in vector) { Statement 1 Statement 2 ……… Statement N }
If you observe the above syntax of for loop, the Vector may be a string, integer, or anything you want to iterate.
- It starts with an Object, which means it will iterate Vector, and then it will assign the first item to a value. For example, values are A: E, which will assign A to val.
- Next, it will execute the statements inside the For loop.
- After completing the statements, it will go to the vector and assign the next value to the val.
- The process will be repeated until there are no items.
For Loop Flow Chart
The screenshot below will show you the flow chart behind the For Loop in R programming.

The execution process of the for loop is:
- Initialization: We initialize the variable(s) here. Example x =1.
- Items in the Sequence/Vector: It will check the items, and if there are items in sequence (True), then it will execute the statements inside the for loop. If there is no item in sequence ( False), then it will exit from that block.
- After completing every iteration, it will traverse to the next item the given number of times.
- Again it will check the new items in the vector. As long as the items are available, the statements inside it will be executed.
R For Loop Example
In this example, we are going to explain how to use this loop to extract individual items or data from a Vector.
In this R Programming example, First, we declared Countries and assigned the following values.
Here, we used the for loop to iterate through the Countries vector and display each individual item present in it.
In the next line, we used a print statement outside of it. This statement will be executed once the compiler exit.
countries <- c('India', 'U K', 'Japan', 'U S A', 'Australia', 'China') for (str in countries) { print(paste("Countries are: ", str)) } print("----This is Coming from Outside ---")

From the above Screenshot, you can observe that we used the Countries List in the second for Loop in R example code.
countries <- c('India', 'U K', 'Japan', 'U S A', 'Australia', 'China')
First Iteration
- First Iteration, Country = ‘India’.
- It means there are some items in the vector, so it will execute the print statement inside the loop.
Second Iteration
- In the second Iteration of the for loop, Country = ‘U K’.
- It means there are some items in the vector, so it will execute the print inside it.
Third Iteration
- Country = ‘Japan’.
- It means there are some items in it, so it executes the statement.
Fourth Iteration
- Country = ‘U S A’.
- It means there are some items in the vector, so it executes the print statement.
Fifth Iteration of an R For Loop
- Country = ‘Australia’, condition tested.
- It means some items exist, so it executes the print statement.
Sixth Iteration
- Country = ‘China’.
- It means there are some items in the vector, so it will compile the print statement inside it.
Next iteration, there are no items in this to assign to the Country variable. So, it will exit.
Example 2
In this for loop in R example, we will explain this working functionality on integer.
numbers <- c(1:10) for (num in numbers) { print(9 * num) } print("---- This is Coming from Outside ---")
