Loops in R

Learn via video courses
Topics Covered

Overview

There are three loops in the R programming language such as the 'for' loop, the 'while' loop, and the 'repeat' loop that are used for iteration. These looping statements are also referred to as control statements that are used to run code multiple times. These loops are present almost in all programming languages like C++, Python, etc. In this, We are going to learn about the loops in r and how these loops work with the help of examples.

Types of Loops in R Programming

There are three types of loops in R programming language, which we are going to discuss in this article:

  • For loop
  • While loop
  • Repeat loop

Let's discuss all these loops in R programming language one by one with some examples and flow diagrams.

1. For Loop

For loop is the most commonly used loop in every programming language. If you want to repeat a particular task for some specific number of times, then "for" loop is used.

In R language whenever you want to repeat a task or an element in sequence like lists, vectors, etc. for some specific number of times, for this purpose "for" loop is a very basic statement. The "for" loop is a type of control loop used in R to run a statement or set of statements multiple times, but before the iteration, there are some conditions that are checked first, such as how far the loop will go and whether the element is in the range of the sequence or not..

If the condition is true then the statements in the body gets executed, if the condition is false then the body will not be executed. Each item in the sequence is iterated by the "for" loop.

Let's see the Syntax of the R-For loop.


Syntax

  • Value in sequence: It refers to the sequence of data which you want to iterate.
  • statement: This specifies the body that contains code that you want to iterate.

Flow Diagram

loop flow diagram

In this flow chart, the working of the 'for' loop is demonstrated. First, an iterator iterates on each item one-by-one in the sequence and then checks if it is the last item of the sequence or not. If the current value of an iterator is the last value of the sequence, then loops will stop; otherwise, the iterator will go inside the loop and repeat the whole process for the next value.

Below are some examples of the 'for' loop which helps to understand the working of the 'for' loop by using this flow chart.


Examples

  • Example 1:

    Output:

    Explanation:

    In the above code example, we try to print numbers from 1 to 10 using for loop. The iterator 'i' is used to iterate one by one on every number according to the mentioned range. First 'i' iterator on 1 then it goes inside the print statement and prints the current holding value of i Then the value of the 'i' iterator gets incremented and it becomes 2 in the next iteration then it prints 2. In this 'i' one by one goes on every number until it gets the last element and prints the value.

  • Example 2:

    Output:

    Explanation:

    In the above code example, assigning the vector of strings to the 'Fruits' variable Then, inside the 'for' loop, an iterator 'f' is used to iterate over each word present inside the vector and print it. Like, the first iterator is on the first string of a vector, which is "Apple", It goes inside the loop, and there is a command to print the current value of the iterator, so it prints it, and then the iterator moves to the next string in the vector and repeats the process for each string.

  • Example 3:

    Output:

  • Example 4:

    Output:

    Explanation:

    In the above code example, a list of numbers is assigned to the variable 'my_list'. Inside the 'for' loop iterator 'i' is used to iterate over the list and one by one print each element of the list.

2. While Loop

The while loop in R language is used to run the same code multiple times. In the while loop, it is not known how many times the loop should run. The same code block is going to run again and again until it finds the stop condition.. It keeps running the same code until a stop condition is achieved. Before executing the code block in the "while" loop, first check the condition. This is why in the while loop, if the loop is going to be run n times, the while loop checks the condition for true or false n+1 times. .


Syntax

  • condition: Condition on which basis your statement gets executed.
  • Statement: This specifies the body inside the loop that contains code which you want to iterate.
  • update_expression: The iterator needs to be updated to move to the next element.

Flow Diagram while loop flow diagram

In this flow diagram, the working of the 'while loop' is demonstrated. The first step is to check the condition for the current value of the iterator. If the condition is false, then simply break the loop; otherwise, go inside the loop, execute the statement for the current value of the iterator, then iterate back to the loop and repeat the process for the next value.


Examples

  • Example 1:

    Output:

    Explanation:

    In the above code example, numbers from 1 to 5 get printed by using the 'while' loop. Firstly, the variable num is initialized by 1, and then the while loop applies the condition that if 'num' is less than or equal to 5, print the current value of "num" and then increment the "num" variable by one.

  • Example 2:

    Output:

    Explanation:

    The above code is for finding the sum of n numbers using a while loop. Firstly 0 is assigned to the variable 'sum', 'i' is initiated with 1, and 5 is assigned to the variable 'n'. Inside the while loop condition is checked that if 'i' is less than equal to 'n' only then go inside the loop and execute the statement. Inside the loop when the condition is satisfied, the sum variable gets updated as the value that 'i' is currently holding is also added to the sum. Previous the sum is 0 after ist iteration the sum becomes 1 then it becomes 2 and so on.

3. Repeat Loop

In R the repeat loop is very similar to the do-while loop in other languages. Repeat loop is used to iterate over a code multiple times. until the break statement is found, it repeatedly executes the same code again and again. The loop will terminate or exit the loop by using a break statement. Without a break statement, it will result in an indefinite loop. The block of the code is going to be executed at least one time in a repeat loop.


Syntax

  • condition: Condition on which basis your statement gets executed.
  • Statement: This specifies the body that contains code that you want to iterate.
  • Break: It is used to exit from the loop.

Flow Diagram

repeat loop flow diagram

The above flow diagram represents the working of the 'repeat' loop. First of all the statement gets executed at one time then the condition is checked. if the condition is satisfied break the loop otherwise repeat the process for the next iteration. This loop gets executed at least one time.


Examples

  • Example 1:

    Output:

    Explanation: In the above code example, value 1 is stored in the 'num' variable, and the 'repeat' loop gets started. Inside the loop firstly print command is gets executed the variable 'num' gets incremented by one After that 'if' loop is started inside this loop the condition is going to be checked that if the num becomes greater than 5 then break the loop otherwise execute the statement.

  • Example 2:

    Output:

    Explanation: In this code, the sum of 'n numbers is calculated by using a 'repeat' loop. The values 0 and 1 are assigned to the variables 'sum' and 'i'. Inside the 'repeat' loop 'sum' variable gets updated by the addition of 'i' and 'i' gets incremented by 1 in each iteration. Whenever the condition inside the 'if' loop becomes false i.e. the value of 'i' becomes greater than 5, it will break the loop.

Jump Statements in Loop

To skip the particular iteration in the loop or at a particular iteration if you want to terminate the loop then these jump statements are used. There are two types of jump statements in loops that are commonly used almost in every language. These are:

  • Break Statement
  • Next Statement

Let's see how what these states are and how it works with some examples.

Break Statement

Whenever you want to exit from the loop or terminate the loop, the jump statement break is used for this purpose. Let's see how it works with the help of the below example.

Code:

Output:

Explanation:

In the above code example, the "for" loop is used to print the elements that lie in the range 1 to n. In the first step, "i" holds the value 1, then the condition inside the "if loops gets checked, that is, whether "i" is equal to 5 or not. If the value of "i' is not equal to 5, then print it; otherwise, break the loop and exit.

Next Statement

To skip the specific iteration in the loop 'next' jump statement is used. Let's see how it works with the help of the below example.

Code:

Output:

Explanation: In the above code, the "for" loop is used to print the items that lie in the range of 1 to 10, but with some conditions that are mentioned inside the "if" statement. Inside the "if" loop, the condition is checked first to see if the current value of iterator "i" is equal to 5 or not. If it is equal to 5, then don't print it; simply continue to the next value.

Conclusion

  • In R programming language there are three types of loops in R as 'for' loop, 'while' loop, and 'Repeat' loop.
  • All the loops are used to iterate over a sequence multiple times according to the different conditions.
  • The 'For' loop is used to iterate on each element of the sequence a specified number of times.
  • In the 'While' loop, if you want to iterate over a sequence for n number of times, then the condition will be checked for n+1 number of times. The "while loop" first checks the condition and then executes the code block by iterating over the items in the sequence.
  • The 'Repeat' loop is very similar to the do while loop in other languages. The code block statement gets executed at least one time.
  • Repeat loop uses 'break' statement to exit the loop.
  • The jump statements are the statements that are used to exit or skip some iteration from the loop based on the provided condition.
  • There are two types of jump statements in R programming languages, i.e., the 'break' jump statement and the 'next' jump statement.
  • The 'break' jump statement is used to exit from the loop, and the 'next' jump statement is used to skip some specific iteration from the loop.