A repeat loop is used to iterate a block of code. It is a special type of loop in which there is no condition to exit from the loop. For exiting, we include a break statement with a user-defined condition. This property of the loop makes it different from the other loops.
A repeat loop constructs with the help of the repeat keyword in R. It is very easy to construct an infinite loop in R.
The basic syntax of the repeat loop is as follows:
Flowchart
- First, we have to initialize our variables than it will enter into the Repeat loop.
- This loop will execute the group of statements inside the loop.
- After that, we have to use any expression inside the loop to exit.
- It will check for the condition. It will execute a break statement to exit from the loop
- If the condition is true.
- The statements inside the repeat loop will be executed again if the condition is false.
Example 1:
Output
Example 2:
Output
Example 3: Infinity repeat loop
Output
Example 4: repeat loop with next
Output
Example 5:
Output
No comments:
Post a Comment