## Introduction

In this chapter, you will learn about the `while`

loop in R. The `while`

loop is a control structure that allows you to repeatedly execute a block of code as long as a specified condition is `TRUE`

. Understanding how to use the `while`

loop is essential for performing tasks that require repeated execution based on a condition.

## The while Loop

### Basic Usage

The `while`

loop in R continues to execute a block of code as long as the specified condition remains `TRUE`

. If the condition becomes `FALSE`

, the loop stops.

### Syntax

```
while (condition) {
# Code to execute while the condition is TRUE
}
```

`condition`

: A logical expression that is evaluated before each iteration. If it evaluates to`TRUE`

, the loop continues; if it evaluates to`FALSE`

, the loop stops.

## Examples

### Example 1: Basic while Loop

In this example, the `while`

loop prints numbers from 1 to 5.

**Example**:

```
# Basic while loop
i <- 1
while (i <= 5) {
print(i)
i <- i + 1
}
# Output:
# [1] 1
# [1] 2
# [1] 3
# [1] 4
# [1] 5
```

### Example 2: Summing Numbers Using while Loop

In this example, the `while`

loop calculates the sum of numbers from 1 to 10.

**Example**:

```
# Summing numbers using while loop
sum <- 0
i <- 1
while (i <= 10) {
sum <- sum + i
i <- i + 1
}
print(paste("The sum is:", sum))
# Output: [1] "The sum is: 55"
```

### Example 3: Using break in a while Loop

In this example, the `break`

statement is used to exit the loop when a specific condition is met.

**Example**:

```
# Using break in a while loop
i <- 1
while (TRUE) {
if (i > 5) {
break # Exit the loop
}
print(i)
i <- i + 1
}
# Output:
# [1] 1
# [1] 2
# [1] 3
# [1] 4
# [1] 5
```

### Example 4: Using next in a while Loop

In this example, the `next`

statement is used to skip the current iteration when a specific condition is met.

**Example**:

```
# Using next in a while loop
i <- 1
while (i <= 10) {
i <- i + 1
if (i %% 2 == 0) {
next # Skip even numbers
}
print(i)
}
# Output:
# [1] 3
# [1] 5
# [1] 7
# [1] 9
# [1] 11
```

## Example Program with while Loop

Here is an example program that demonstrates the use of the `while`

loop in R:

```
# R Program to Demonstrate while Loop
# Using while loop to calculate factorial of a number
factorial <- function(n) {
result <- 1
i <- 1
while (i <= n) {
result <- result * i
i <- i + 1
}
return(result)
}
# Calculate factorial of 5
n <- 5
fact <- factorial(n)
# Print the factorial
print(paste("The factorial of", n, "is:", fact))
# Output: [1] "The factorial of 5 is: 120"
# Using while loop to find the first number greater than 100 that is divisible by 7
num <- 1
while (TRUE) {
if (num > 100 && num %% 7 == 0) {
print(paste("The first number greater than 100 that is divisible by 7 is:", num))
break
}
num <- num + 1
}
# Output: [1] "The first number greater than 100 that is divisible by 7 is: 105"
```

## Conclusion

In this chapter, you learned about the `while`

loop in R, including how to use it for basic iteration, summing numbers, and more complex tasks like calculating factorials and finding specific numbers. You also learned how to incorporate the `break`

and `next`

statements within a `while`

loop. The `while`

loop is used for performing tasks that require repeated execution based on a condition. By mastering the `while`

loop, you can write more dynamic and efficient R programs.