R While Loop

To execute a block of code more than once in a continuous loop, the while keyword is utilized in R, similar to the repeat statement. However, unlike the repeat statement, the while statement presents a condition to exit from the loop. However, an infinite loop can also be executed using the while loop.

Syntax:

while (test_expression) {
   statement
}

Example 1:

var <- 2
while(var!=10){
      print(var)
      var = var+2
}

Output:

[1] 2
 
[1] 4
 
[1] 6
 
[1] 8

Example 2:

num <- 0
while (num < 10) {
  num <- num + 1
  if (num == 3) {
    next
  }
  print(num)
}

Output:

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

Example 3:

dice <- 0
while (dice < 6) {
  dice <- dice + 1
  if (dice < 6) {
    print("FAIL")
    next
  }
  print("WINNER!")

Output:

[1] "FAIL"
 
[1] "FAIL"
 
[1] "FAIL"
 
[1] "FAIL"
 
[1] "FAIL"
 
[1] "WINNER!"

Example 4:

j<-0
while(j<1000){
    if (j==500){
        j=j+100
        next
    }
    cat("\nValue =",j)
    j=j+100

Output:

Value = 0
 
Value = 100
 
Value = 200
 
Value = 300
 
Value = 400
 
Value = 600
 
Value = 700
 
Value = 800
 
Value = 900

Example 5:

num <- 1
while (num <=10) {
  print(num)
  num <- num + 1
  if (num == 10) {
    break
  }
}

Output:

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

Example 6:

var <- c("Hello","World!!")
Count <- 1
while (Count < 10) {
   print(var)
   Count = Count + 2
}

Output:

[1] "Hello" "World!!"
 
[1] "Hello" "World!!"
 
[1] "Hello" "World!!"
 
[1] "Hello" "World!!"
 
[1] "Hello" "World!!"

Example 7:

num <-55
num <- as.integer(num)
sum <-0
while(num!=0){
    sum = sum + (num%%10)
    num = as.integer(num/10)
}
cat("Sum of the digits of 55 =",sum)

Output:

Sum of the digits of 55 = 10

Example 8: Palindrome Number:

var <- 5432345
var <- as.integer(var)
N <- var
reverse_N <- 0
while(var!=0){
    num <- var%%10
    reverse_N <- num + (reverse_N*10)
    var <- as.integer(var/10)
}
cat("N =", N)
cat("\nreverse_N =", reverse_N)
if(reverse_N==N){
    cat("\n\nN = reverse_N\n",N,"is a Palindrome Number\n\n")
}else{
    cat("\n\nN != reverse_N\n",N,"is not a Palindrome Number\n\n")
}
 
var <- 4321
var <- as.integer(var)
N <- var
reverse_N <- 0
while(var!=0){
    num <- var%%10
    reverse_N <- num + (reverse_N*10)
    var <- as.integer(var/10)
}
cat("\n\nN =", N)
cat("\nreverse_N =", reverse_N)
if(reverse_N==N){
    cat("\n\nN = reverse_N\n",N,"is a Palindrome Number")
}else{
    cat("\n\nN != reverse_N\n",N,"is not a Palindrome Number")
}

Output:

N = 5432345
 
reverse_N = 5432345
 
N = reverse_N
 
5432345 is a Palindrome Number
 
N = 4321
 
reverse_N = 1234
 
N != reverse_N
 
4321 is not a Palindrome Number

Example 9: Armstrong Number:

N = 371
sum = 0
var = N
while(var > 0) {
    num = var %% 10
    sum = sum + (num ^ 3)
    var = floor(var / 10)
}
if(N == sum) {
   cat(N, "is an Armstrong Number!")
} else {
    cat(N, "is not an Armstrong Number!")
}
 
N = 100
sum = 0
var = N
while(var > 0) {
    num = var %% 10
    sum = sum + (num ^ 3)
    var = floor(var / 10)
}
cat("\n")
if(N == sum) {
   cat(N, "is an Armstrong Number!")
} else {
    cat(N, "is not an Armstrong Number!")
}

Output:

371 is an Armstrong Number!
 
100 is not an Armstrong Number!

Example 10: Frequency of a digit in a number:

x = 1000000000
N = as.integer(x)
digit = 0
var = N
count = 0
while(N > 0) {
        if(N%%10==digit){
            count = count+1
        }
        N = as.integer(N/10)
}
cat("The Frequency of",digit,"in",var,"=",count)

Output:

The Frequency of 0 in 1000000000 = 9
Please follow and like us:
Content Protection by DMCA.com