B.Sc Computer Science
R Programming - Lab
Bharathiar University
Program 6
Loops in R
Program 6- Write a R program to demonstrate various types of loops
SOURCE CODE:
# R program to demonstrate loops
# application of for loop
week <- c('Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday')
for (day in week)
{
print(day)
}
# R program to demonstrate the use of while loop
x = 1
while (x <= 5)
{
cat("2 x ", x,"=", x*2,"\n")
x = x + 1
}
# R program to illustrate
# the application of repeat loop
i <- 0
repeat
{
print("Basic to pro tamil!")
i = i + 1
if (i == 5)
{
break
}
}
OUTPUT:
> # R program to demonstrate loops
>
> # application of for loop
> week <- c('Sunday',
+ 'Monday',
+ 'Tuesday',
+ 'Wednesday',
+ 'Thursday',
+ 'Friday',
+ 'Saturday')
>
>
> for (day in week)
+ {
+ print(day)
+ }
[1] "Sunday"
[1] "Monday"
[1] "Tuesday"
[1] "Wednesday"
[1] "Thursday"
[1] "Friday"
[1] "Saturday"
>
> # R program to demonstrate the use of while loop
>
> x = 1
> while (x <= 5)
+ {
+
+ cat("2 x ", x,"=", x*2,"\n")
+ x = x + 1
+ }
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
>
> # R program to illustrate
> # the application of repeat loop
>
> i <- 0
> repeat
+ {
+ print("Basic to pro tamil!")
+ i = i + 1
+ if (i == 5)
+ {
+ break
+ }
+ }
[1] "Basic to pro tamil!"
[1] "Basic to pro tamil!"
[1] "Basic to pro tamil!"
[1] "Basic to pro tamil!"
[1] "Basic to pro tamil!"
| |
|
No comments:
Post a Comment