You are on page 1of 3

PROG 5 LOOPING CONSTRUCTS IN R

AIM:

To perform looping operations in R.

ALGORITHM:

1. Initialize variables fruit and v to store inputs .


2. Input is given as a list using vector function c()
3. For loop syntax:
for (initialization_Statement; test_Expression; update_Statement)

{
// statements inside the body of the loop
}
4. Repeat loop syntax:
repeat {
commands
if(condition) {
break
}
}
5. While loop syntax:
while (test_expression) {
statement
}

SOURCE CODE:

R For loop

# Create fruit vector


fruit <- c('Apple', 'Orange',"Guava", 'Pinapple', 'Banana','Grapes')
# Create the for statement
for ( i in fruit){
print(i)
}
R Repeat loop
v <- c("Hello","repeat","loop")
cnt <- 2
repeat {
print(v)
cnt <- cnt+1

if(cnt > 5) {
break
}
}

R while loop

v <- c("Hello","while loop","example")


cnt <- 2
while (cnt < 7) {
print(v)
cnt = cnt + 1
}

OUTPUT:

R FOR LOOP

R REPEAT LOOP
R WHILE LOOP

RESULT:

Thus the program is completed and the output is verified successfully.

You might also like