0% found this document useful (0 votes)
1 views4 pages

Loop in R Program

The document provides an overview of loops in R programming, including for loops, while loops, and repeat loops, along with their syntax and examples. It explains how to control the flow of execution within loops using control statements like break and next. The document includes sample code for each type of loop and demonstrates their functionality through practical examples.

Uploaded by

Royal Harshit
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)
1 views4 pages

Loop in R Program

The document provides an overview of loops in R programming, including for loops, while loops, and repeat loops, along with their syntax and examples. It explains how to control the flow of execution within loops using control statements like break and next. The document includes sample code for each type of loop and demonstrates their functionality through practical examples.

Uploaded by

Royal Harshit
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

"Inv

thE
print(paste("You
selected

making
conditions,

Money"
Output : Withdraw if-else
selected multiple
"You replaces
efficiently

ecsO oool o
switch)
Here, maintal.s Of
and easier to
execution
repetitive

that allow
conGition
LoOPS INRPROGRAMMING

programming unti a
in
instructions

of a set of
calculations
constructs
fundamental execution perfoming
Loops are allow
repeated
of a vector,
R, loops over elements
code [Link] are used for iterating
is satisfied. They
and automating.

wve knOw the


Theto Loop iterations is known, i.e., when
when the number of
The for loop is used required.
eract number of iterations
Syntax
for (variable in sequence) {
statemnents

6.7
Conditions and Control Flow, Loops, Functions and the apply family in R
Example : Write an R program using a for loop to print the square of numbers trom 1
to 5.
Solution:
for (i in l :5) {
print(i^2)

Output :
[1]1
[1]4
[1]9
(1] 16
[1]25
The while Loop
The while loop continues execution as long as the condition holds TRUE. It is useful
when the number of iterations is unknown beforehand.
Syatax :
CoolodO bievnt
while (condition )

statement oMwssbtiw bolool

Example :Write an R program using a while loop to print numbers from 1 to 5


Solution:
#Input
while (i <= 5)( #While loopolls agpol
print(i)
i<i+1

Output :
(1]1
(1]4 beipes olos o
(1) 16
(1)25

6.8
Business Analytics
Rscreen output :The output for (i) for loop and (ü) while loop is as :
Oeng stu tures RStudio
ode Ve Phti Sesean Busld Deug Protte look Help
Addrm

Source on Save
Souce
4- for (1 in 1:$) (
print(1A2)

hle (1
rint(1)
5) (

442 CAeShnetriertetog/Bies Aratytics Bokotroeng strutur


Por (1 in 1:5) {
prinr(1a2)

The Repeat Loop


The repeat loop runs indefinitely until terminated by [Link] the repeatloop
we use abreak keyword. Repeat loop does not check for acondition before execution but
is used.
keeps on executing the code indefinitely until a break
Syntax :
repeat {
statement
iI (condition) (
break

6.9
Conditions and Control Flow, Loops, Functions and the apply family in R
Example:Write an R program using a repeat loop to display numbers rom lto 8and
stop the loop using the break statement.
Solution :
i<-l
repeat {
print(i)
i<-i+l
if (i>5) break

Output :
(1) 1
(1]4
[1]9
(1] 16
(1] 25
Loop Control Statements
To control the flow of execution within loops, loop control statements are used. They
help decide when to continue, skip or terminate a loop during execution of the code. Two
types of loop control statements are used :
Break :It is used to terminate the loop immediately.
Syatax :
break
Next :It is used toskip the current iteration and move to next iteration in the loop.
Syntax :
next
Example :Write an R program using a for loop to print numbers from l to 6, skipping
the number 3 using the next statement.
Solution:
lor (i in 1 :6)(eto eodbetio
it(( ==3) next
print(1)

Output
[1] 1
[1]2
(1]4
[1]8
[1]6
6.10 Business Analytics

You might also like