Loops in R Language

R Language | Loops: In this tutorial, we are going to learn about the various types of Loops in R programming language with their working, syntax and examples.
Submitted by Bhavya Sri Khandrika, on May 05, 2020

Sometimes the programmers need to encounter a situation where they need to execute a particular block of code many times. Everyone is acquainted with the fact that usually whenever there is a block of code then the statements in them are executed sequentially. However, if a situation exists where the user needs to run the same block multiple times then it will be difficult to write the same code for those many times as needed. Therefore keeping the advancing requirements and needs of the users in the programming world certain functions are inbuilt in any programming language to ensure the comfortable atmosphere around the programmers. The above-stated functions are nothing but so-called loops in any programming language.

Loops

Coming to the brief description of loops, loops are inbuilt functions that run the same block of code repeatedly. Commonly a loop statement allows the users to execute a statement or a block of statements multiple times. In loops, we generally follow a particular format in which there are several parameters like conditional along with conditional code when the conditions inserted become either true or false. The following is the general chart of the above-mentioned parameters. Coming to the brief description of loops, loops are inbuilt functions that run the same block of code repeatedly. Commonly a loop statement allows the users to execute a statement or a block of statements multiple times. In loops, we generally follow a particular format in which there are several parameters like conditional along with conditional code when the conditions inserted become either true or false. The following is the general chart of the above-mentioned parameters. 

The users working with the R will get a chance to acknowledge the different types of loops and loop statements.

There are mainly three types of loops. They are:

  1. repeat Loop
  2. while Loop
  3. for Loop

1. repeat Loop

Using the repeat loop statement a particular group of statements can be executed repeatedly. And, on the other hand, it also shortens the code written.

Syntax:

    repeat {   
       //code to be executed   
       if(exit_condition) {  
          break  
       }  
    }

Initially, we ought to initialize some variables which are taken by the repeat loop and thus are processed. Along with the initialization variables, one should also declare a condition in the repeat loop. This specific condition is checked and later the group of statements is executed as per the boolean obtained after checking the condition. Finally, the repeat loop uses a break statement for coming out of the repeat loop.

Have a look at the following example which depicts the usage of the repeat loop in the R language.

Example:

# Initialising the counter and then 
# using it in the repeat loop
i <- 1 
repeat { 
  print("Welcome to includehelp.com") 
  i <- i+1     
  if(i >= 4) { 
     break 
  } 
}

Output

[1] "Welcome to includehelp.com"
[1] "Welcome to includehelp.com"
[1] "Welcome to includehelp.com"

2. while Loop

In the while loop, the block of statements will be repeatedly executed till the entry_condition is true. But prior to that, the while condition is checked and it must turn.

Syntax:

    while (entry_condition) {  
       //Code to be executed  
    } 

In general, a while Loop is called an "entry controlled loop" in programming languages. This is because prior to the execution of the block of statements the while loop initially checks for the trueness in the condition and then if the condition becomes true the statements are executed sequentially.

Example:

i <- 1
while (i <= 10) {
 print(i*i)
 i = i+1
}

Output

[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
[1] 36
[1] 49
[1] 64
[1] 81
[1] 100

3. for Loop

The main purpose of the for loop is the same as the while loop, it checks the condition prior to before executing the group of statements. On the other hand, we also have loop control statements along with the different types of loop statements.

Syntax:

    for (valuetoIterate) {  
        //code to be executed 
    } 

Here the above syntax helps the users in making use of the for loop in R. To support the flow of the while loop now let us consider an example and apply the above concept to it.

Example:

A<- c(432, 35, 45, 14)

print("Values of the vector:")
for ( i in A){  
  print(i) 
}

Output

[1] "Values of the vector:"
[1] 432
[1] 35
[1] 45
[1] 14


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.