Lec9 - Programming in R - Control Structures
Lec9 - Programming in R - Control Structures
Control structures
In this lecture
if-else-if family
for loop
Nested for loops
for loop with if break
while
Control structures
Sequence function
• A sequence is one of the components of a ‘for loop’
• Sequence function syntax : seq(from, to, by, length)
• Creates equi-spaced points between ‘from’ and ‘to’
Initializing sum=0
loop variable
1 1
2 3
3 6
4 10
5 15
While loop
A while loop is used whenever you want to execute statements until a specific
condition is violated
Consider the sequence of natural numbers.
What is the value of the natural number up to which the calculated sum is less
than specified “Fin_sum”?
1+2+3…..+n = Fin_sum (15)