Programming for Data Analysis
(CT127-3-2-PFDA and Version VC1)
Control Statements and Loops
Topic & Structure of the lesson
-if and else
- Switch
- If else
- For Loop
- While Loop
- Repeat
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/Oand Loops
Control Statements SlideSlide
2 of 255of 17
Learning outcomes
At the end of this topic, you should be able to:
• Understand the various data control structures
in the R Programming
• Understand the different loops available in the
R language
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/Oand Loops
Control Statements SlideSlide
3 of 355of 17
Key terms you must be able to use
- If you have mastered this topic, you should
be able to use the following terms correctly
in your assignments and exams:
- if and else
- switch
- ifelse
-for
- while
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/Oand Loops
Control Statements SlideSlide
4 of 455of 17
Control Statements
- Control Statements allow us to control the flow
of our programming and cause different things to
happen depending on the values of tests
- Test results in a logical,TRUE or FALSE
- The main control statements are
- If
- else
- ifelse
- switch
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/Oand Loops
Control Statements SlideSlide
5 of 555of 17
Slide ‹#› of 9
Control statement
if
-It is one of the control statements in R programming that
consists of a Boolean expression and a set of statements.
- If the Boolean expression evaluates to TRUE, the set of
statements is executed.
- If the Boolean expression evaluates to FALSE, the
statements after the end of the If statement are
executed.
- The basic syntax for the if statement is given below:
if(Boolean_expression) {
This block of code will execute if the Boolean expression returns TRUE.
}
Eg: x <- “Intellipaat”
if (is.character(x)) {
print("X is a Character")
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis } File I/Oand Loops
Control Statements SlideSlide
6 of 655of 17
else
In the If -Else statement, an If statement is followed by an
Else statement, which contains a block of code to be
executed when the Boolean expression in the If the
statement evaluates to FALSE.
- The basic syntax of it is given below:
if(Boolean_expression) {
This block of code executes if returns TRUE.
} else {
This block of code executes if returns FALSE.
}
Eg: x <- c("Intellipaat", "R", "Tutorial")
if("Intellipaat" %in% x) {
print("Intellipaat")
} else {
print("Not found")
}
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/Oand Loops
Control Statements SlideSlide
7 of 755of 17
If-else-if
-Multiple Else-If statements can be included after an If statement. Once
an If statement or an Else if statement evaluates to TRUE, none of the
remaining Else if or Else statement will be evaluated.
-The basic syntax of it is given below:
if(Boolean_expression1) {
This block of code executes if the Boolean expression 1 returns TRUE
} else if(Boolean_expression2) {
This block of code executes if the Boolean expression 2 returns TRUE
} else if(Boolean_expression3) {
This block of code executes if the Boolean expression returns TRUE
} else {
This block of code executes if none of the Boolean expression returns TRUE
}
Eg: x <- c("Intellipaat","R","Tutorial")
if("Intellipaat" %in% x) {
print("Intellipaat")
} else if ("Tutorial" %in% x)
print("Tutorial")
} else {
print("Not found")}
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/Oand Loops
Control Statements SlideSlide
8 of 855of 17
Switch
- Switch statement is one of the control statements in R
programming which is used to equate a variable against
a set of values.
- Each value is called a case.
Basic syntax for a switch statement is as follows:
switch(expression, case1, case2, case3....)
Eg: x <- switch( 3, "Intellipaat", "R", "Tutorial",
"Beginners")
print(x)
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/Oand Loops
Control Statements SlideSlide
9 of 955of 17
Loops
- The function of a looping statement is to
execute a block of code, several times
- It provide various control structures that
allow for more complicated execution
paths than a usual sequential execution.
- The types of loops in R are repeat, while
and for
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/Oand Loops
Control Statements SlideSlide
10 of1055of 17
repeat
- A repeat loop is one of the control statements in R
programming that executes a set of statements in a loop
until the exit condition specified in the loop, evaluates to
TRUE.
- Basic syntax for a repeat loop is given below:
repeat {
statements
if(exit_condition) {
break
}
}
Eg: v <- 9
repeat {
print(v)
v=v-1
if(v < 1) {
break }}
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/Oand Loops
Control Statements SlideSlide
11 of1155of 17
While
- A while loop is one of the control statements in R
programming which executes a set of statements in a
loop until the condition (the Boolean expression)
evaluates to TRUE.
- Basic syntax of a while loop is given below
while (Boolean_expression) {
statement
}
Eg:
v <-9
while(v>5){
print(v)
v = v-1
}
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/Oand Loops
Control Statements SlideSlide
12 of1255of 17
for
- For loop is one of the control statements in R
programming that executes a set of statements in a loop
for a specific number of times, as per the vector provided
to it.
- Basic syntax of a for loop is given below
for (value in vector) {
statements
}
Eg:
v <- c(1:5)
for (i in v) {
print(i)
}
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/Oand Loops
Control Statements SlideSlide
13 of1355of 17
Quick Review Questions
• What are the different control statements
available in R Programming
• What are the different Loops available in R
Programming
• What is the use of break statement
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/Oand Loops
Control Statements SlideSlide
14 of1455of 17
Summary of Main Teaching Points
• Control statements
-if
-else
-if else if
-switch
• Loops
-repeat
- while
- for
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/Oand Loops
Control Statements SlideSlide
15 of1555of 17
Q&A
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/Oand Loops
Control Statements SlideSlide
16 of1655of 17
Next Session
• Functions
Create functions
Function Arguments
Return values
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/Oand Loops
Control Statements SlideSlide
17 of1755of 17