![]() |
VOOZH | about |
In R Programming Language, we require a control structure to run a block of code multiple times. Loops come in the class of the most fundamental and strong programming concepts. A loop is a control statement that allows multiple executions of a statement or a set of statements.
The word βloopingβ means cycling or iterating. Jump statements are used in loops to terminate the loop at a particular iteration or to skip a particular iteration in the loop. The two most commonly used jump statements in loops are:
Note: In R language continue statement is referred to as the next statement.
The basic function of the Break and Next statement is to alter the running loop in the program and flow the control outside of the loop. In R language, repeat, for, and a while loops are used to run the statement or get the desired output N a number of times until the given condition to the loop becomes false.
Sometimes there will be such a condition where we need to terminate the loop to continue with the rest of the program. In such cases R Break statement is used. Sometimes there will be such condition where we don't want loop to perform the program for specific condition inside the loop. In such cases, R next statement is used.
The break Statement in R is a jump statement that is used to terminate the loop at a particular iteration.
Syntax:
if (test_expression) {
break
}
Output:
[1] "Values are: 1"
[1] "Values are: 2"
[1] "Values are: 3"
[1] "Values are: 4"
[1] "Coming out from for loop Where i = 5"
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
The next statement in R is used to skip the current iteration in the loop and move to the next iteration without exiting from the loop itself.
Syntax:
if (test_condition)
{
next
}
Output:
[1] "Values are: 1"
[1] "Values are: 2"
[1] "Values are: 3"
[1] "Values are: 4"
[1] "Values are: 5"
[1] "Skipping for loop Where i = 6"
[1] "Values are: 7"
[1] "Values are: 8"
[1] "Values are: 9"
[1] "Values are: 10"
Output:
[1] 2
[1] 4
[1] 5