In the previous article of this series, we have learned about what control structures are, why they are introduced under R, how they help in better decision making as well as looping to save time for a programmer, and some control structures in detail with examples. If you haven’t read that article yet, it can be found at Control Structures in R : Part 1.
In this article, we are going to have a look at the remaining control structures. Talking more specifically, we are going to cover while loops, break statements, next statements, and repeat loops in R programming respectively.
We have seen the for loop which repeats a certain task for the given set of condition/s (if it holds). On the similar lines, we have a while loop in R. A while loop works in a simple manner. It repeats a certain block of code until the given condition is TRUE. As soon as the condition is FALSE (or not satisfying), the loop ends or exits.
The rule on which while loop works is, evaluate the expressions as long as the given condition is satisfied. It will keep the loop running until the condition holds.
Syntax for a while loop is as shown below:
The syntax for while loop in R
Let us see an example of a while loop that allows us to have a better understanding of the loop.
Image 1: Example code for while loop
Here, we have initialized a variable named “base” with initial value as zero. Then, under the while loop, we have added a condition that will check if the value for “base” is less than or equals 10 at each iteration. As long as the condition holds, we will keep printing the value for “base” by increasing its value by 1 at every iteration of the code.
On the 12th iteration of the code, the condition will not hold (base = 10 + 1 will return 11, which is not less than or equals to 10) and the while loop will be terminated automatically.
Let’s see an output of the code below.
Image 2: Output for the while loop
A break statement is considered as a control structure that allows the programmer to control the flow of any loop (for loop, while loop, repeat loop). This statement allows you to break the loop immediately and come out of it. This statement is really useful when we are having a nested looping structure.
Imagine a situation where you have a for loop running for specific criteria and you want to come out of the loop as soon as a certain condition is TRUE within the loop. You can use a combination of if and break statements together to get this done.
You can check and evaluate the condition where you want to stop the for loop using an if statement. Under the block of code for the if statement, you can place the break statement and it will break the loop immediately. Finally, you can choose to do something after the loop breaks. Let’s see an example below for a better realization.
Image 3: Example code for break statement
In the example above, we are applying a for loop for numbers 1 to 50. Inside the for loop, if the value exceeds 20, we wanted the loop to be broken. Therefore, we used a break statement under the if clause.
After the loop breaks, we wanted it to print out the values the loop returns. Therefore, we have used the print statement after break. Let’s see the output for this code. It ideally should print the first 20 integers on your screen.
Image 4: Output for the break statement
This is how the break statement works as a control structure that controls the flow of any program under R. We can precisely say that this statement is useful to immediately terminate the loop and execute the next statements on the line.
After we successfully went through the break statement that controls the flow of a loop, here is time to check one more statement that controls the flow of any loop but in exactly the opposite manner of that break statement.
The next statement under R controls the flow of any program in a way that it allows the user to skip the current iteration from a loop and move towards the next one without actually terminating the loop.
I mean, imagine a situation where you actually wanted the loop to skip a certain iteration based on a logical condition and then move towards the next iteration without terminating the loop. The next statement comes to your rescue in such situations. This statement is usually used under nested loops to control the execution flow.
Let us see an example for a better understanding of the next statement under R.
Image 5: Example code for next statement
In this example, we want to iterate the numbers 1 to 10 under for loop but not the number 5. Therefore, we have added an if clause and next statement to skip the fifth number from iteration.
Now, the for loop starts and under it, if statement checks if the value is 5 or not. If the value does not equal to five, it allows the for loop to proceed further and print it. However, when the value is 5, it skips that iteration of for loop and starts a fresh iteration under for loop.
The final output of the code if you see, doesn't contain the number 5 in it.
Image 6: Output for next statement
This is how the next statement works under R Programming.
We have seen a for loop which first checks the condition and then repeats the statement if the condition holds. However, we may come up in situations where we need to repeat the statements but don’t have any condition for them to check. In such cases, for loop, while loop doesn’t help at all.
Taking into consideration the scenarios like this, R has the repeat loop which repeats the statements from the word GO! You don’t need to specify any condition to repeat the statements. But the thing we should note is, a repeat loop is an infinite loop by default and there is only one way to come out of that loop; using a break statement!
Syntax for the repeat loop is as shown below:
Syntax for repeat loop in R
Let's see an example of a repeat loop in R programming.
Image 7: Example code for the repeat loop
In this example, we have initiated a variable with base value as 10. Under repeat statement, we are printing the result of every x when it has been reduced by one unit. This is an infinite loop. To break the same, we have used a condition that specifies that as soon as the value of x becomes zero, we need to break this loop.
This loop should print the values from 1 to 9 in reverse order. Let’s see if it does the same in output.
Image 8: Output for the repeat loop
This is how the return loop works under R. It doesn’t require any condition to start looping and is an infinite loop by default unless otherwise, you add a break statement to it.
The while loop repeats the statement until a certain condition is TRUE. As soon as the condition is FALSE or doesn’t hold, it stops the loop.
The break and next statements are developed to have control under the programmer's hand.
The break statement allows the programmer to break the loop for a certain iteration without terminating the loop.
The next statement allows the programmer to skip a certain iteration within a for, while, or repeat loop without terminating the loop.
The repeat is a unique loop under R that allows the user to iterate certain statements without specifying any condition in contrast to for and while loops. However, the thing that users should keep in mind is adding a break statement to terminate this loop.
We will stop here and join you guys next time with some more decent and advanced topics under R programming. Stay tuned for the same, you can read out some other blogs related to data science and technologies at analyticssteps.com. See you guys in my next article on this trail. Until then, Stay Safe! 😊
5 Factors Influencing Consumer Behavior
READ MOREElasticity of Demand and its Types
READ MOREAn Overview of Descriptive Analysis
READ MOREWhat is PESTLE Analysis? Everything you need to know about it
READ MOREWhat is Managerial Economics? Definition, Types, Nature, Principles, and Scope
READ MORE5 Factors Affecting the Price Elasticity of Demand (PED)
READ MORE6 Major Branches of Artificial Intelligence (AI)
READ MOREScope of Managerial Economics
READ MOREDifferent Types of Research Methods
READ MOREDijkstra’s Algorithm: The Shortest Path Algorithm
READ MORE
Latest Comments