DharaneeshRajasekara
386 views
16 slides
Jan 11, 2022
Slide 1 of 16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
About This Presentation
In computer programming, a loop is a sequence of instructions that are continually repeated until a certain condition is reached. In this we learn about loops in R language
Size: 395.2 KB
Language: en
Added: Jan 11, 2022
Slides: 16 pages
Slide Content
Loops In R - R. M. Dharaneesh
Contents Loops For loops Nested for loops While loops break Repeat loops next
Loops In computer programming, a loop is a sequence of instructions that are continually repeated until a certain condition is reached. Loops can either be entry-controlled or exit-controlled. Entry-controlled loops run only if the condition for the looping process stays true. Exit-controlled loops run at least once, regardless of the condition, conditions are checked for only at the end of the loop block.
Loop Functioning
For Loops For loops are an entry-controlled type of loops, i.e., conditions are given on the entry of the loop. If the conditions are not satisfied, then the loop doesn't execute. Otherwise, the loop runs until the condition becomes false. It is used to iterate over the items of a sequence, usually only when the range of the sequence is known. Update statements are not needed as the loop automatically updates the counter variable.
For Loops - Example
Nested For Loops For loops can be nested, i.e., for loops can be placed inside of other for loops. If for example, the outer loop will make 'm' iterations and the inner loop will make 'n' iterations, then the total number of iterations made will be m * n.
Nested For Loops - Example
While Loops While loops are another entry-controlled type of loop in R. While loops are usually used when the total number of iterations are not known, although they can also be used when the number of iterations are known. Update statements have to be explicitly mentioned in while loops.
While Loops - Example
Break Keyword The 'break' keyword is used to 'break' out of a loop abruptly in between its execution. Once a 'break' statement is encountered in a loop, the control exits out of the loop completely.
Repeat Loops It is a simple loop that will run the same statement or a group of statements repeatedly until the stop condition has been encountered. Repeat loop does not have any condition to terminate the loop, a programmer must specifically place a condition within the loop’s body and use the declaration of a break statement to terminate this loop. If no condition is present in the body of the repeat loop then it will iterate infinitely.
Repeat Loops - Example
Next Keyword The 'next' keyword is used to skip the current iteration in a loop. Once the 'next' keyword is encountered in a loop, the control will instantly skip that iteration.