0% found this document useful (0 votes)
6 views7 pages

Lab 6

The document discusses the three types of loops in R programming: for loops, while loops, and repeat loops. For loops iterate over elements in a sequence. While loops run until a condition is false. Repeat loops run statements repeatedly until a break condition is encountered.

Uploaded by

amodedipesh12
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)
6 views7 pages

Lab 6

The document discusses the three types of loops in R programming: for loops, while loops, and repeat loops. For loops iterate over elements in a sequence. While loops run until a condition is false. Repeat loops run statements repeatedly until a break condition is encountered.

Uploaded by

amodedipesh12
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
You are on page 1/ 7

LAB- 6:

IMPLEMENTATION OF R LOOPS

1.1 LOOPS IN R:
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.

A loop asks a query, in the loop structure. If the answer to that query requires an action, it will be
executed. The same query is asked again and again until further action is taken. Any time the query is
asked in the loop, it is known as an iteration of the loop. There are two components of a loop, the
control statement, and the loop body. The control statement controls the execution of statements
depending on the condition and the loop body consists of the set of statements to be executed.

1.2 TYPES OF LOOPS:

There are three types of loops in R programming:

• For Loop
• While Loop
• Repeat Loop

1.2.1 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.

for (val in 1: 5)

{
# statement
print(val)
}
• OUTPUT-.

[1] 1

[1] 2

[1] 3

[1] 4

[1] 5

• EXAMPLE: 2- Program to display days of the week

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"

1.2.2 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 )
{ statemen
t}

• EXAMPLE: 1- Program to calculate


the factorial of a number.
n <- 5

# assigning the factorial variable

# and iteration variable to 1

factorial <- 1 i <- 1

# using while loop

while (i <= n)

# multiplying the factorial variable

# with the iteration variable

factorial = factorial * i

# incrementing the iteration variable

i=i+1

# displaying the factorial

print(factorial)

• OUTPUT-.
[1] 120

• EXAMPLE: 2- Program to display


numbers from 1 to 5 using a while
loop in R. 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

1.2.3 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.

• SYNTAX-

R – Repeat loop Syntax:

repeat

statement

if( condition )

break
}

• EXAMPLE: 1- Program to display a statement five times.

repeat

# statement to be executed multiple times

print("Repeat Loops!")

# incrementing the iteration variable

i=i+1

# checking the stop condition

if (i == 5)

# using break statement

# to terminate the loop

break

• OUTPUT-.
[1] " Repeat Loops!"
[1] " Repeat Loops!"
[1] " Repeat Loops!"
[1] " Repeat Loops!"
[1] " Repeat Loops!"
• EXAMPLE: 2- Program to display numbers from 1 to 5 using a repeat loop in R
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

You might also like