0% found this document useful (0 votes)
15 views11 pages

R Programming: Conditions and Loops

Uploaded by

smitata173
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views11 pages

R Programming: Conditions and Loops

Uploaded by

smitata173
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Unit I : Conditions and Loops in R

If statement is one of the Decision-making statements in the R


programming language.
- It is one of the easiest decision-making statements. It is used
to decide whether a certain statement or block of statements
will be executed or not i.e if a certain condition is true then a
block of statement is executed otherwise not.
Syntax:
if (expression) {
#statement to execute if condition is true
}
If the expression is true, the statement gets executed. But if the
expression is FALSE, nothing happens. The expression can be a
logical/numerical vector, but only the first element is taken into
consideration. In the case of numeric vector, zero is taken as
FALSE, rest as TRUE.
Example of if statement in R
# R program to illustrate if statement

# assigning value to variable a


a <- 5
# condition
if(a > 0)
{
print("Positive Number") # Statement
}
Example 2: R if statement with optional argument
# Assigning value to variable x
x <- 12
# Condition
if (x > 20)
{
print("12 is less than 20") # Statement
}
print("Hello World")
Example 3: Python if…else statement
# R program to illustrate if statement
# assigning value to variable a
a <- -5
# condition
if(a > 0)
{
print("Positive Number") # Statement
}else{
print("-ve number")
}
Loops in R (for, while, repeat)
In R programming, we require a control structure to run a block of
code multiple times. Loops come in the class of the most
fundamental and strong programming concepts. A loop is a
control statement that allows multiple executions of a statement
or a set of statements. The word ‘looping’ means cycling or
iterating.
For Loop in R
It is a type of control statement that enables one to easily
construct an R loop that has to run statements or a set of
statements multiple times. For R loop is commonly used to iterate
over items of a sequence. It is an entry-controlled loop, in this
loop, the test condition is tested first, then the body of the loop is
executed, the loop body would not be executed if the test
condition is false.
R – For loop Syntax:
for (value in sequence)
{
statement
}
Example 1: Program to display numbers from 1 to 5 using for loop
in R.
# R program to demonstrate the use of for loop
# using for loop
for (val in 1: 5)
{
# statement
print(val)
}
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
Here, for loop is iterated over a sequence having numbers from 1
to 5. In each iteration, each item of the sequence is displayed.
Example 2: Program to display days of the week.
# R program to illustrate
# application of for loop

# assigning strings to the vector


week <- c('Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday')

# using for loop to iterate


# over each string in the vector
for (day in week)
{

# displaying each string in the vector


print(day)
}
Output:

[1] "Sunday"
[1] "Monday"
[1] "Tuesday"
[1] "Wednesday"
[1] "Thursday"
[1] "Friday"
[1] "Saturday"
In the above program, initially, all the days(strings) of the week
are assigned to the vector week. Then for loop is used to iterate
over each string in a week. In each iteration, each day of the week
is displayed.
R For-Loop on a List
# Create a list of numbers
my_list <- list(1, 2, 3, 4, 5)
# Loop through the list and print each element
for (i in seq_along(my_list)) {
current_element <- my_list[[i]]
print(paste("The current element is:", current_element))
}
Output
[1] "The current element is: 1"
[1] "The current element is: 2"
[1] "The current element is: 3"
[1] "The current element is: 4"
[1] "The current element is: 5"
We have a list of five numbers in this case. The seq_along()
function is used to create a list of indices to loop through, and
double brackets [[]] are used to retrieve the current element
during each loop iteration. We output a message showing the
element we’re dealing with inside the loop, followed by the value
of that element.
While Loop in R
It is a type of control statement that will run a statement or a set
of statements repeatedly unless the given condition becomes
false. It is also an entry-controlled loop, in this loop, the test
condition is tested first, then the body of the loop is executed, the
loop body would not be executed if the test condition is false.
R – While loop Syntax:
while ( condition )
{
statement
}
Example 1: Program to display numbers from 1 to 5 using a while
loop in R.
# R program to demonstrate the use of while loop
val = 1
# using while loop
while (val <= 5)
{
# statements
print(val)
val = val + 1
}
Output:

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
Initially, the variable value is initialized to 1. In each iteration of
the while loop the condition is checked and the value of val is
displayed and then it is incremented until it becomes 5 and the
condition becomes false, the loop is terminated.
Repeat Loop in R
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.
R – Repeat loop Syntax:
repeat
{
statement

if( condition )
{
break
}
}
To terminate the repeat loop, we use a jump statement that is the
break keyword.
Example 1: Program to display numbers from 1 to 5 using a repeat
loop in R.
# R program to demonstrate the use of repeat loop
val = 1

# using repeat loop


repeat
{
# statements
print(val)
val = val + 1

# checking stop condition


if(val > 5)
{
# using break statement
# to terminate the loop
break
}
}
Output:

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
In the above program, the variable val is initialized to 1, then in
each iteration of the repeat loop the value of val is displayed and
then it is incremented until it becomes greater than 5. If the value
of val becomes greater than 5 then a break statement is used to
terminate the loop.
Jump Statements in Loop
We use a jump statement in loops to terminate the loop at a
particular iteration or to skip a particular iteration in the loop.
The two most commonly used jump statements in loops are:
Break Statement: The break keyword is a jump statement that is
used to terminate the loop at a particular iteration.
Example:
# R program to illustrate
# the use of break statement

# using for loop


# to iterate over a sequence
for (val in 1: 5)
{
# checking condition
if (val == 3)
{
# using break keyword
break
}

# displaying items in the sequence


print(val)
}
Output:

[1] 1
[1] 2
In the above program, if the value of val becomes 3 then the break
statement will be executed and the loop will terminate.
Next Statement: The next keyword is a jump statement which is
used to skip a particular iteration in the loop.
# R program to illustrate
# the use of next statement

# using for loop


# to iterate over the sequence
for (val in 1: 5)
{
# checking condition
if (val == 3)
{
# using next keyword
next
}

# displaying items in the sequence


print(val)
}
Output:
[1] 1
[1] 2
[1] 4
[1] 5
In the above program, if the value of Val becomes 3 then the next
statement will be executed hence the current iteration of the loop
will be skipped. So 3 is not displayed in the output.

As we can conclude from the above two programs the basic


difference between the two jump statements is that the break
statement terminates the loop and the next statement skips a
particular iteration of the loop.

You might also like