R Programming - Switch Statement and Looping Techniques.

R Programming - Switch Statement and Looping Techniques.

A program for statistical analysis - Part 4.

Hitendra K's photo
Hitendra K
·Jun 2, 2022·

3 min read

Subscribe to our newsletter and never miss any upcoming articles

Play this article

In the previous blog, we explored control structures and output functions. In this blog, we can learn about the Switch statement, which is also a type of control structure, and in addition to that, we will also see about loops.

Switch statement

The switch is similar to the if statement as it evaluates a test condition and, if it is true, it performs the operations listed in between the curly braces while the switch statement has various test cases if one of the test cases is matched with the given expression, the operations specified in the particular case are executed.

Syntax

switch ( Test expression, case 1, case 2...)


var <- switch(

          2,

          "Hi",

          "Quasar",

          "Community"

)

print (var)



//output

"Quasar"

Loops

Loops are special statements which allow us to execute certain statements multiple times. There are looping statements in most programming languages. R programming provides the following looping techniques for loop, while loop and repeat loop.

For loop:

For loop is an entry controlled loop, it checks the expression at the start of the program itself so that the body of the loop is executed if the test condition is evaluated true and skips the body of the loop if it isn't true. The for loop can be used in situations where iteration for lists, dataframes, vectors, matrices etc. For loop, it iterates through a set of elements for a specified number of times. The loop checks the test condition for each iteration so the required condition is met.

Syntax

for(var in list)

{

body of the loop

}


for ( i in 5:6)

{

      print ( i-1)

}



//output

4

5

Nested for loop:

Nested for loop is the type of looping technique where two more loops are executed within each loop. These are effective to iterate elements into matrices, 2-dimensional arrays etc.


for ( var in 2:4)

{

        for(j in 5:7)

        {

              print ( var * j )

         }

}



//output

10

12

14

15

18

21

20

24

28

While loop

While loop is also an entry check loop. The test condition is evaluated first and if it's true, the body of the loop is executed. This loop is used when the number of iterations is not known. The loop is executed till the test condition is evaluated to be false.

Syntax

while( Test condition )

{

Body of the loop

Incrementation or decrementation (Update expression)

}


var <- ("Quasar community")

i <- 1



while ( i<4)

{

           print(var)

           i=i+1

}



//output

"Quasar community"

"Quasar community"

"Quasar community"

Repeat loop

This looping technique is used to iterate a block of code multiple times. This code gets executed continuously until a break statement is found. The difference between a repeat loop and other looping techniques is that there is no need for an end condition. Instead, it looks for a break statement.

Syntax

repeat

{

Block of code

if (condition)

{

break

}

}


var <- ("Quasar community")

x <- 1

repeat

{

      print(var)

      i <- i+1



      if (i >3)

      {

            break

      }

}



//output

"Quasar community"

"Quasar community"

Some Good to Know Statements in R

Break Statement

This statement is used to terminate a loop or control statement at the required iteration.

Next Statement

This statement is used to skip to the next iteration by discontinuing the current iteration.

In the next blog, we will learn about functions, types of functions etc. Follow the Quasar community for more interesting blogs.

 
Share this