Control Structures in R

In the previous tutorials, we learned about different types of data structures used in R such as vectors, lists, matrices, arrays, etc. Now it is time to understand how to identify through a column vector of data values to check whether it meets a certain condition or only executes a function if it meets a certain condition. So we need control structures or control statements that allow controlling the flow of a program.

In R programming language there are few control structures to control the flow of a program or to simply control flow statements. The most useful and simplest control flow statements in R are loops and decision-making statements. 

What are control structures in R programming?

In any programming language, the decision-making is implemented using some control structures or control flow statements. In simple terms, the control flow structures support to control of the flow of execution in R source code.

The program begins the execution sequentially from the first statement of the R source code. The control structures control the sequential flow with conditional statements like branching and iteration. Branching allows the currently executing piece of code(block)  to jump to another part of the same program which results in a deviation from the default execution behavior.In this tutorial, we concentrate on learning the branching statements used to control the flow like If, if-else, nested else-if, switch, etc.

Control structures like for loop allow the simplification of code by avoiding repeated execution of the same code multiple times. It is often described by the term iteration. Iteration is a control structure that uses loops that allows to repeatedly execute a block (a piece of a program) of a statement till a condition applied is satisfying. In our next tutorial, we will discuss the for loop control structure in the R programming language.

What are the types of control structures in R?

The different types of control structures to control the flow of execution in the R programming language are given in the below table with a short description. In the coming sessions of this tutorial, you will learn in detail each control structure with examples. The picture depicts the available control structures in the R programming language.

Control Structures in R

The types of control structures can be grouped into three

  1. Condition statements in R: The conditional statements further includes
    1. If statements
    2. If……else statements
    3. Nested if…….else
    4. Switch
  2. Loops in R programming: The loops in R consist of structures like
    1. for loop
    2. while loop
    3. repeat loop
  3. Jump Statements in R Programming
    1. Break statement
    2. Next statement
    3. Return statements
No Types of control structures Description
1 If and else To test whether a given condition is true or false
2 While loop To execute a loop when a condition is satisfied.
3 Repeat loop To repeat the statement iteration
4 break To exit from a statement in iteration or loop
5 next To omit the current iteration and jumps to the next iteration cycle without terminating a loop.
6 return To return the result after execution
7 for-loop To iterate over a statement (loop) multiple times.

In the coming tutorials, we are going to learn about the control structures like condition statements, loops, and jump statements and their syntax and characteristics in detail.