EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials R Programming Tutorial While Loop in R
 

While Loop in R

Priya Pedamkar
Article byPriya Pedamkar

Updated July 5, 2023

While Loop in R

 

 

Introduction to While Loop in R

A while loop concept in R programming, which has its syntax and starts with the keyword “while,” much like in most other programming languages, has a block structure inside which statements to be executed are specified and which continue to execute, operating with requisite variables or constants are set by the user, based on the context, until a certain condition is met, after which control of execution passes out of the block that acts a loop that has a conditional end.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Control Flow

Control Flow 1

While Loop in R

Below is an example of using While loop statements.

Control Flow 2

Let’s say we are unsure how many times we must repeat an action or expression to be executed. In such cases, we use the While statement with the logical condition.

With FOR loop, we use curly brackets to wrap the expressions. If it is a single expression, curly brackets aren’t required.

Control Flow

Control Flow 3

Syntax:

While(condition)
expression
while(condition){
expression 1
expression 2
…
}

Example

a <- 1
b <- 2
while (b > 1){
c <- a + b
b <- 0
print(c)
}

Output:

[1] 3

Steps
  1. We have initialized a to 1 and b to 2 in the above example.
  2. In the while statement: We have a condition to check if b is greater than 1.
  3. We then enter the loop as the condition (b>1) is true.
  4. We add both a and b and store them in the resultant variable C.
  5. Print c.

Infinite Loop Sequence

While using the while statement, we must be cautious in defining the condition/statements. Otherwise, we may end up in an infinite loop.

Example 

while (b > 1)
{
c <- a + b
b <- 0
print(c)
}

Removing the statement (B <- 0) from the program will lead to an infinite loop because b is defined as 2 at the start and never changes its value through the program. Unless we change its value in the loop. (b <- 0) .This allows the program to print C only once and not infinite times.

Remember, all the statements in FOR and WHILE Loop are executed sequentially.

Loop Control Statements

There are two different types of loop control statements in R.

  • Break
  • Next

Break statement

The function of the break statement is to bring the execution out of the loop and execute the statements outside the loop, if any.

Syntax:

While (condition)
{
Expression 1
Break
}

Example

a <- 1
b <- 2
while (b > 1)
{
c <- a + b
b <- 0
print(c)
break
}
print(b)

Output:

[1] 3

[1] 0

Steps
  1. Using the same while example program, we have added a break statement after print statements.
  2. We have used the break to come out of the loop and print b.

Next Statement

We use the NEXT statement to skip a statement in the loop.

Syntax:

While (condition)
{
Expression 1
next
skip statement
}

Example

a <- 1
b <- 2
while (b > 1){
c <- a + b
b <- 0
next
print(c)
}
print(b)

Output:

[1] 0

Steps
  1. We used the NEXT statement after b <- 0
  2. NEXT statement skips the statement (print(c)) and prints b.

Recommended Articles

This is a guide to While Loop In R. Here, we discuss the introduction to While Loop In R and different types of loops in R, along with some examples and steps. You may also have a look at the following articles to learn more –

  1. R Programming Language
  2. Careers in R Programming
  3. Data Science Career

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW