EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials R Programming Tutorial While Loop in R
Secondary Sidebar
R programming Tutorial
  • Loops
    • Loops in R
    • For Loop in R
    • Nested For Loop in R
    • While Loop in R
    • Next in R
  • Basic
    • What is R Programming Language
    • Careers in R Programming
    • Install R
    • List of R Packages
    • Introduction of R Tools Technology
    • R Programming Language
    • DataSet in R
    • What is RStudio?
    • R-studio-Functions
    • R Packages
    • Time series?in R
    • R Data Types
    • R for data science
    • R Operators
    • R Data Frame
    • R Analytics Tool
    • R Tree Package
    • Vectors in R
  • Control statement
    • If Statement in R
    • If Else Statement in R
    • Else if in R
    • Switch Statement in R
  • Chart/graphs
    • Graphs in R
    • Bar Charts in R
    • Pie Chart in R
    • Histogram in R
    • Line Graph in R
    • Plot Function in R
    • Scatterplot in R
    • R Boxplot labels
  • Regression in R
    • Simple Linear Regression in R
    • Linear Regression in R
    • Multiple Linear Regression in R
    • Logistic Regression in R
    • Poisson Regression in R
    • OLS Regression in R
    • P-Value in Regression
  • Anova in R
    • ANOVA in R
    • One Way ANOVA in R
    • Two Way ANOVA in R
  • Data Structure
    • R list
    • Arrays in R
    • Data Frames in R
    • Factors in R
    • R Vectors
  • Advanced
    • Statistical Analysis with R
    • R String Functions
    • Data Exploration in R
    • R CSV Files
    • KNN Algorithm in R
    • Sorting in R
    • lm Function in R
    • Hierarchical Clustering in R
    • R Normal Distribution
    • Binomial Distribution in R
    • Decision Tree in R
    • GLM in R
    • Arima Model in R
    • Linear Model in R
    • Predict Function in R
    • Survival Analysis in R
    • Standard Deviation in R
    • Statistical Analysis in R
    • Predictive Analysis?in R
    • T-test in R
    • Database in R
  • Programs
    • Functions in R
    • Boxplot in R
    • R Program Functions
    • Factorial in R
    • Random Number Generator in R
  • Interview question
    • R Interview Questions

While Loop in R

By Priya PedamkarPriya Pedamkar

While Loop in R

Introduction to While Loop in R

A while loop concept in R programming which has its own syntax and starts with the keyword “while”, much like in most other programming languages, having a block structure inside which statements to be executed are specified and which continue to execute, operating with requisite variables or constants are specified 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.

Control Flow

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Control Flow 1

While Loop in R

Below is an example of using While loop statements.

Control Flow 2

Let’s say, we are not sure how many times we need to repeat an action or expression to be executed. In such cases, we make use of 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. In the above example, We have initialized a to 1 and b to 2.
  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 in resultant variable C.
  5. Print c.

Infinite Loop Sequence

While using the while statement, we need to 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)
}

If we remove the statement (B <- 0) from the program then, it 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. R Programming Coaching Online
  3. Careers in R Programming
  4. Data Science Career
Popular Course in this category
R Programming Training (13 Courses, 20+ Projects)
  13 Online Courses |  20 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Statistical Analysis Training (15 Courses, 10+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*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?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more