0% found this document useful (0 votes)
7 views13 pages

1st Class

The document discusses different types of loops in R programming including for, while, and repeat loops. It provides examples of using each loop type to iterate through sequences, find sums and products, and print outputs. The examples demonstrate the basic syntax and usage of each loop.

Uploaded by

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

1st Class

The document discusses different types of loops in R programming including for, while, and repeat loops. It provides examples of using each loop type to iterate through sequences, find sums and products, and print outputs. The examples demonstrate the basic syntax and usage of each loop.

Uploaded by

habibthealien
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Computer Programming with R

Repetitive execution: for loop, while loop, repeat loop

Salma Akter
Lecturer, Department of Statistics
Jagannath University
for loop

• Syntax:
for ( variable in vector) {
statement(s)
}
Ex: 1
for ( i in 1:5) {
print(i^2)
}
for loop

• Ex: 2
storage <-numeric()
for ( i in 1:5) {
storage[i]<-i^2
}
storage
> mean (storage)
• Ex :3
for ( i in 1:3) {
for ( j in 1:2){
print(i+j)
}
}
for loop

Ex :4 Multiply the first 10 natural numbers


prod=1
for ( i in 1:10){
prod=prod*i
}

• Ex: 5 Print the square root of the integers one to ten


for( x in 1:10){
print(sqrt(x))
}
for loop

Ex: 6 Print the sequence 101:105


seq=100
for ( i in 1:5){
seq=seq+i
print(seq)
}

• Ex :7 Finding sum of first 10 natural numbers using for loop


x<-1:10
sum<-0
for ( i in ( 1:10)){
sum<-sum+x[i]
}
while loop

• The while structure evaluates a expression as long as a


stated condition is TRUE.
The syntax is:
while(logical condition){
statement(s)
}
Ex: 1
x<-1
while(x<10){
print(x)
x<-x+1
}
while loop

• Ex:2
x<-1
while(x<10){
x<-x+1
print(x)
}
while loop

• Ex:4 Finding sum of first 10 natural numbers using while loop


Total<-0
n<-1
while(n<=10){
Total<-Total+n
n<-n+1
}
Ex:5 Multiply the first 10 natural numbers
i=1
prod=1
while(i<=10){
prod=prod*i
i=i+1
}
while loop

Ex:6 Write R code using while loop that gives the output like
1 1
1 2
2 1
2 2
Solution:
i<-1
while(i<=2){
j<-1
while(j<=2){
print(c(i,j))
j<-j+1
}
i<-i+1
}
while loop
#### Write R code using while loop that gives the output like
1 1
1 3
3 1
3 3

Break Statement: Stop the while loop


Break statement is a control statement. When R encounters it, the while loop is abandoned completely.
Ex:
x<-1
while(x<7){
x<-x+1
if(x==3)
break
print(x)
}
while loop

next Statement:
next statement is used to skip one step if while loop
Ex:
x<-1
while(x<7){
x<-x+1
if(x==3)
next
print(x)
}
repeat loop
• Another looping structure is repeat, which repeats the
same expression. The syntax is:
repeat{
statement(s)
if(condition) {
break
}
}
Ex: Finding first 5 natural numbers
x<-1
repeat{
print(x)
x=x+1
if(x==6){
break
}
}
repeat loop

Ex: Sum of first 10 natural numbers


i=1
sum=0
repeat{
sum=sum+i
i=i+1
if(i>10){
break
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy