Data science for Engineers
Control structures
Programming in R – Control Structures NPTEL NOC18-CS28 1
Data science for Engineers
In this lecture
if-else-if family
for loop
Nested for loops
for loop with if break
while
Programming in R – Control Structures NPTEL NOC18-CS28 2
Data science for Engineers
Control structures
• Execute certain commands only when
certain condition(s) is satisfied (if-then-else)
• Execute certain commands repeatedly and
use a certain logic to stop the iteration (for,
while loops)
Programming in R – Control Structures NPTEL NOC18-CS28 3
Data science for Engineers
If else family of constructs
If , If else and If-elseif - else are a family of constructs where:
• A condition is first checked, if it is satisfied then operations are performed
• If condition is not satisfied, code exits construct or moves on to other
options
If construct If-else if-else construct
if (condition) { if (condition) {
statements
statements
}
} else if (condition) {
If-else construct
if (condition) { alternate statements
statements } else {
} else { alternate statements
alternate statements
}
}
Programming in R – Control Structures NPTEL NOC18-CS28 4
Data science for Engineers
If else family of constructs: Example
IN THE EXAMPLE BELOW:
• IF x is greater than 7 operations inside the curved braces would occur
• Else the next condition i.e. x >8 would be checked, if this too is not true
• Final else condition is checked and if that too is false, there is no change in ‘x’
Code Console Output
# If-elseif - else example
x=6
if(x>7){
x=x+1
}else if(x>8){
x=x+2
}else {
x=x+3}
Programming in R – Control Structures NPTEL NOC18-CS28 5
Data science for Engineers
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’
Parameter Description Console Output
from starting number
to ending number
by increment or decrement
(width)
length Number of elements
required
Programming in R – Control Structures NPTEL NOC18-CS28 6
Data science for Engineers
for loop, Nested for Loops
The structure of a for loop construct comprises:
• A ‘sequence’ which could be a vector or a list
• ‘iter’ is an element of the sequence
• Statements
Nested for-loop : one or more for loop constructs are located within another.
For loop construct Nested For loop construct
for(iter1 in sequence1) {
for(iter in sequence) { for(iter2 in sequence2) {
statements statements
} }
}
Programming in R – Control Structures NPTEL NOC18-CS28 7
Data science for Engineers
for loop: Example
Open a script file and type the following statements. Save the file as
“forloop” and execute the script file
The value of ‘sum’ keeps changing inside the loop
Initializing sum=0
loop variable
Loop variable sum
(i)
1 1
2 3
3 6
4 10
5 15
Programming in R – Control Structures NPTEL NOC18-CS28 8
Data science for Engineers
For- loop with if-break
A “break” statement once executed, program exits the loop even before iterations
are complete
“break” command comes out of the innermost loop for nested loops
Loop sum Condition
variable (sum>SU
(i) M)
1 1 False
2 3 False
3 6 False
4 10 False
5 15 False
break” command, the loop is
6 21 True
terminated after the 6th iteration
Programming in R – Control Structures NPTEL NOC18-CS28 9
Data science for Engineers
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)
Initialize the Loop variable sum Condition
variables
(i) (sum<Fin_sum)
1 1 True
2 3 True
3 6 True
4 10 True
5 15 False
Programming in R – Control Structures NPTEL NOC18-CS28 10