Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 34

Control Statements in R

Raju N
If Statement
 The syntax of R if statement is:
if (expression)
{
statement
}
 If the expression is TRUE, the statement gets
executed. But if it is FALSE, nothing happens.
Example
x <- 100
if(x > 0)
{
print("This is Positive number")
}
if else statement
 The syntax of if…else statement in R is:
if (expression)
{
statement1
}
else
{
statement2
}
Example
x <- -10.56
if(x >= 0)
{
print("This is Non-negative number")
} else
{
print("This is Negative number")
}
Nested if else statement
 In nested if… else statements we can impose as many else if conditions as we
require
 The syntax of nested if…else statement is:
if (expression1)
{
statement1
}
else if (expression2)
{
statement2
}
else if (expression3)
{
statement3
}
else
statement4
Example 1
x <- 5
if (x < 0)
{
print("This is Negative number")
} else
if (x > 0)
{
print("This is Positive number")
} else
print("This is Zero")
Example 2
• Marks <- 85
• if (Marks >=75)
• {
• print("Passed With First class and Distinction")
• } else
• if (Marks >=60)
• {
• print("Passed In First Class")
• } else
• if (Marks >=50)
• {
• print("Passed In Second Class")
• }else
• if (Marks >=35)
• {
• print ("Passed in Third Class")
• }else
• print("Failed")
For loop in R

 Syntax of simple for loop in R


for(i in 1:n)
{
statement
}
Example
for(i in 1:5)
{
print (i^2)
}
Indices need not be sequential...

for(i in c(-5,-3,0,1,2,3,4,1.3))
{
print(i^2)
}
Aliter

x= c(-5,-3,0,1,2,3,4,1.3)
for(i in x){print (i^2)}
i and i^2 together

for(i in c(-5,-3,0,1,2,3,4,1.3)){print (c(i, i^2))}


Storing the generated values
data=numeric(5)
data
for(i in 1:5){data[i]=i^2}
data
Length in Meter to length in CM
for(lenm in c(3,2.75,.9))
{lenincm=lenm*100
print(c(lenm,lenincm))}
Nested For Loops
 Syntax of Nested for loop in R:
for(i in 1:n)
{
for(j in 1:n)
{
statement
}
}
Example
for(i in 1:5)
{
for(j in 3:5)
{
print(i*j)
}
}
Break Statement in for loops

• A break statement is used inside a loop  to


stop the iterations and flow the control
outside of the loop.
Example
x <- 1:5
for (i in x) {
if (i == 3){
break
}
print(i)
}
The Use of “next” in for Loop

• “next” discontinues a particular iteration and


jumps to the next cycle.
• In fact, it jumps to the evaluation of the
condition holding the current loop.
Example
x <- 1:5
for (i in x) {
if (i == 3){
next
}
print(i)
}
While loop in R

 While loop in R repeats the specific block of code until the


condition is no longer satisfied.
 Syntax of while loop in R:
while (condition)
{
statement
}
o First the condition is evaluated and only when it holds TRUE,
While loop enters the body.
o The statements inside the loop are executed and the flow
returns to evaluate the condition again.
While
• This is repeated until condition evaluates to
FALSE, once the condition holds FALSE, the
while loop is exited.
Example
i <- 1
while (i <=6) {
print(i*i)
i = i+1
}
while loop with Break statement:

• break statement will end the loop abruptly.


• Once the break statement is read, the loop
will be terminated.
Example
i <- 1
while (i <= 6) {
if (i==4)
break;
print(i*i)
i = i+1
}
while loop with Next statement:

• Next statement will skip one step of the loop.


• Once the next statement is read, the loop will
skip the while once.
Example
i <- 1
while (i <= 6) {
if (i==4)
{
i=i+1
next;
}
print(i*i);
i = i+1;
}
Repeat Function   

 The Repeat Function(loop) in R executes a


same block of code iteratively until a stop
condition is met.
 Syntax for Repeat Function
repeat {
if(condition) {
break
}
}
repeat
• repeat loop in R will execute a block of
commands repeatedly till break.
Example
sum <- 0
repeat{
sum = sum+1
print(sum)
if (sum == 6){
print("repeat loop ends");
break
}
}
Replicate Function

 rep() is the function in R that replicates the


values
 Syntax for Replicate Function in R:
rep(value,number_of_times)
rep(sequence,each,number_of_times)
Example
1. rep(4,10)
4 is replicated 10 times
2. rep(1:4,len=20)
sequence 1 to 4 is replicated until the
length reaches 20 elements

You might also like