R: Control Flow II

Control flow (or flow of control) is simply the order in which we code and have our statements evaluated. These allow you to control the flow of execution of a script typically inside of a function. There are two types of Control Flow Statement.

1. Single Iterative statements.

2. Multiple Iterative statements.

We already discussed about Single Iterative statement. Now we will discuss on the topic multiple iterative statement.

Multiple Iterative statement:

1. Repeat statement

A repeat loop is used to iterate over a block of code multiple number of times.

We must ourselves put a condition explicitly inside the body of the loop and use the break statement to exit the loop. Failing to do so will result into an infinite loop.

Syntax

Repeat

{

Statement Break

}

In the statement block, we must use the Break statement to exit the loop.

Flowchart of Repeat Loop

Example

Output

In the above example, we have used if condition to check and break the loop when time takes the value of 12.
Hence, we see in our output that Good morning gets printed 4 times.

2. While statement

In R programming, while loops are used to loop until a specific condition is met.

Syntax

While (test_expression)

{

Statements

}

Here, test_expression is evaluated and the body of the loop is entered if the result is TRUE. The Statements inside the loop are executed and the flow returns to evaluate the test_expression again.
This is repeated each time until test_expression evaluates to FALSE, in which case, the loop exits.

Note: We can use Break anywhere in statements module to break the loop when the desired condition is reached .Break arguments are typically embedded in an if statement in which a condition is assessed, if TRUE break out of the loop, if FALSE continue on with the loop

Flowchart of While Loop

 

Example

Output

In the above example, i is initially initialized to 1.
Here, the test_expression is i < 6 which evaluates to TRUE since 1 is less than 6. So, the body of the loop is entered and i is printed and incremented.
Incrementing i is important as this will eventually meet the exit condition. Failing to do so will result into an infinite loop.
In the next iteration, the value of i is 2 and the loop continues.
This will continue until i take the value 6. The condition 6 < 6 will give FALSE and the while loop finally exits.

3. For Loop

A for loop is used to iterate over a vector.

Syntax

For (val in sequence)

{

Statement

}

Here, sequence is a vector and val takes on each of its value during the loop. In each iteration, statement is evaluated.

Note: We can use Break anywhere in statements module to break the loop when the desired condition is reached .Break arguments are typically embedded in an if statement in which a condition is assessed, if TRUE break out of the loop, if FALSE continue on with the loop.
Flowchart of For Loop

Example

Find square between 1 to 10

Output

In the above example, the loop iterates 10 times as the vector is from 1 to 10.

In each iteration, i takes on the value from 1 to 10 in a sequence.

We found the square of the number and printed it in the output.

Thus the loop exists when it gets i=11 and the condition gets FALSE.

4. Nested For Loop

The placing of one loop inside the body of another loop is called nesting. When you “nest” two loops, the outer loop takes control of the number of complete repetitions of the inner loop. Thus inner loop is executed N- times for every execution of Outer loop.

Syntax

For (val1 in sequence)

{

       For (val2 in sequence)

                {

                     Statement

                }

}

Here, sequence is a vector and val1 takes on each of its value during the outer loop and enters the inner loop where val2 takes on each of its value during the inner loop and exits till it gets FALSE condition.

Note: We can use Break anywhere in statements module to break the loop when the desired condition is reached. Break arguments are typically embedded in an if statement in which a condition is assessed, if TRUE break out of the loop, if FALSE continue on with the loop. In a nested looping situation, where there is a loop inside another loop, this statement exits from the innermost loop that is being evaluated.

Flowchart of Nested For Loop

Example

Output

In the above example, the outer loop iterates 5 times as the vector is from 1 to 5.In each iteration, i takes on the value from 1 to 5 in a sequence and enters the inner loop.

The inner loop iterates 2 times as the vector is from 1 to 2.In each iteration, j takes on the value from 1 to 2 in a sequence.

We multiplied i*j and printed it in the output.

Thus the inner loop exists when it gets j=2 and the condition gets FALSE then it goes back to the outer loop and exits when it gets i=5 and the condition gets FALSE

Loop Control Statement

Break statement

A break statement is used inside a loop (repeat, for, while) to stop the iterations and flow the control outside of the loop.
In a nested looping situation, where there is a loop inside another loop, this statement exits from the innermost loop that is being evaluated.
Break arguments are typically embedded in an if statement in which a condition is assessed, if TRUE break out of the loop, if FALSE continue on with the loop

Syntax
Break

Flowchart of Break Statement

Example

Output

In this example, we iterate over the vector x, which has consecutive numbers from 1 to 5.
Inside the for loop we have used a if condition to break if the current value is equal to 3.
As we can see from the output, the loop terminates when it encounters the break statement.

 

Next Statement

A next statement is useful when we want to skip the current iteration of a loop without terminating it. On encountering next, the R parser skips further evaluation and starts next iteration of the loop.

Syntax
Next

Flowchart of Next Statement

Example

Output

In the above example, we use the next statement inside a condition to check if the value is equal to 3.
If the value is equal to 3, the current evaluation stops (value is not printed) but the loop continues with the next iteration.
The output reflects this situation.

References:

https://www.programiz.com/r-programming/for-loop

http://uc-r.github.io/control_statements

https://www.w3schools.in/r/conditional-statements/

About Pabitra Sundar Kashyap:

Pabitra is M.Sc in Computer Science. Currently he is working as Analyst Intern with NikhilGuru Consulting Analytics Service LLP (Nikhil Analytics), Bangalore.

 

Please follow and like us:

Be the first to comment on "R: Control Flow II"

Leave a comment

Your email address will not be published.


*


Subscribe for Data Analytics Edge Newsletter & Share..:-)

error: Content is protected !!