Iterative Statements in R

 

Iterative Statements in R

Introduction

Iterative statements, also known as looping statements, are used to repeatedly execute a block of code until a specified condition is satisfied. Loops eliminate the need to write the same statements multiple times and make programs more compact and efficient.

R provides three looping constructs:

  1. for Loop
  2. while Loop
  3. repeat Loop

R also provides two loop control statements:

  1. break Statement
  2. next Statement

1. for Loop

Definition

The for loop is used to execute a block of code repeatedly for each value in a sequence, vector, list, or other iterable object.

Syntax

for(variable in sequence)
{
statements
}

where:

  • variable represents the loop variable.
  • sequence specifies the values over which the loop iterates.
  • statements are executed once for each value in the sequence.

Flowchart

          Sequence
|
Assign next value
|
Execute statements
|
More values available?
Yes No
| |
Repeat Stop

Example 1: Print Numbers from 1 to 5

for(i in 1:5)
{
print(i)
}

Output

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

Example 2: Find Squares of Numbers

for(i in 1:5)
{
print(i^2)
}

Output

[1] 1
[1] 4
[1] 9
[1] 16
[1] 25

Applications

  • Printing tables.
  • Processing vectors and matrices.
  • Repeating operations a fixed number of times.

2. while Loop

Definition

The while loop repeatedly executes a block of code as long as a specified condition remains TRUE.

Syntax

while(condition)
{
statements
}

Flowchart

        Condition
|
+----+----+
| |
TRUE FALSE
| |
Execute Stop
statements
|
Repeat

Example 1: Print Numbers from 1 to 5

i <- 1

while(i <= 5)
{
print(i)
i <- i + 1
}

Output

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

Example 2: Sum of First Five Natural Numbers

i <- 1
sum <- 0

while(i <= 5)
{
sum <- sum + i
i <- i + 1
}

print(sum)

Output

[1] 15

Applications

  • Unknown number of iterations.
  • Reading data until a condition is met.
  • Menu-driven programs.

3. repeat Loop

Definition

The repeat loop repeatedly executes a block of code indefinitely. The loop terminates only when a break statement is encountered.

Syntax

repeat
{
statements

if(condition)
break
}

Flowchart

       Execute statements
|
Termination condition?
Yes No
| |
break Repeat

Example 1: Print Numbers from 1 to 5

i <- 1

repeat
{
print(i)

i <- i + 1

if(i > 5)
break
}

Output

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

Example 2: Sum of Natural Numbers

i <- 1
sum <- 0

repeat
{
sum <- sum + i
i <- i + 1

if(i > 5)
break
}

print(sum)

Output

[1] 15

4. break Statement

Definition

The break statement is used to terminate a loop immediately. Control passes to the statement following the loop.

Syntax

break

Example 1: Using break in a for Loop

for(i in 1:10)
{
if(i == 6)
break

print(i)
}

Output

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

The loop terminates when i becomes 6.


Example 2: Using break in a while Loop

i <- 1

while(TRUE)
{
print(i)

if(i == 5)
break

i <- i + 1
}

Output

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

Applications of break

  • Exiting loops prematurely.
  • Stopping infinite loops.
  • Terminating loops when a desired condition is achieved.

5. next Statement

Definition

The next statement skips the remaining statements in the current iteration and proceeds to the next iteration of the loop.

Syntax

next

Example 1: Skip Number 5

for(i in 1:10)
{
if(i == 5)
next

print(i)
}

Output

[1] 1
[1] 2
[1] 3
[1] 4
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10

Number 5 is skipped.


Example 2: Print Odd Numbers Only

for(i in 1:10)
{
if(i %% 2 == 0)
next

print(i)
}

Output

[1] 1
[1] 3
[1] 5
[1] 7
[1] 9

Applications of next

  • Skipping unwanted values.
  • Ignoring invalid data.
  • Filtering elements during iteration.

Comparison of Looping Statements

Featureforwhile    repeat
Number of iterations known beforehand        Yes    No    No
Condition checked before execution        Implicit        Yes    No
Infinite loop possible        No    Yes    Yes
Requires break to terminate        No    No    Yes

Comparison of break and next

StatementEffect
breakTerminates the loop completely
nextSkips the current iteration and continues with the next iteration

Summary

StatementPurpose
for    Repeat statements over a sequence
while    Repeat while a condition remains TRUE
repeat    Infinite loop until terminated by break
break    Exit the loop immediately
next    Skip the current iteration

Conclusion

Looping statements in R provide mechanisms for executing a block of code repeatedly. The for loop is suitable when the number of iterations is known, the while loop is useful when repetition depends on a condition, and the repeat loop supports indefinite repetition until explicitly terminated. The break statement terminates a loop, whereas the next statement skips the current iteration and continues with the next one. These constructs are essential for developing efficient and flexible R programs.

Comments

Popular posts from this blog

Statistical Methods Lab ( R Language) PCCBL308 Semester 3 KTU BTech CB and CU 2024 Scheme - Dr Binu V P

Programs in R - using control statements - Assignment 2

Basic R Programs to Try - Assignment 1