Nested for Loop to Print a Pattern.

A nested for loop in R is used when we want to iterate through multiple dimensions, such as rows and columns of a matrix, or for creating patterns. we will discuss how to print multiplication tables up to a certain number with its working example in the R Programming Language using R for loop conditions. Everyone found an easy way for pattern printing in other programming languages. But it’s a tough task using the R language

Approach:

  • Start the first loop for the number of lines
  • Start the second loop for the item to be displayed
  • Print item
  • Increment the second loop until a condition is reached
  • Increment the first loop until the condition is true
  • Continue in this fashion

Syntax:

for (i in 1:n) {
for (j in 1:m) {
# Pattern generation logic
}
}

Example 1: Printing a right-angle triangle

R




n <- 5
for (i in 1:n) {
  for (j in 1:i) {
    cat("* ")
  }
  cat("\n")
}


Output:

* 
* *
* * *
* * * *
* * * * *
  • Initialize n=5, indicating the number of rows in the pattern.
  • The outer loop iterates over the range of i from 1 to n (inclusive), controlling the number of rows in the pattern.
  • Inside the outer loop, the inner loop iterates over the range of j from 1 to the value of i (inclusive). This loop controls the number of asterisks to be printed in each row.
  • Inside the inner loop, cat(“* “) prints an asterisk followed by a space, generating the pattern elements.
  • After the inner loop, cat(“\n”) is used to print a newline character, moving to the next line after printing the pattern for each row.

Example 2: Printing a rectangular pattern

R




n <- 4
m <- 6
for (i in 1:n) {
  for (j in 1:m) {
    cat("* ")
  }
  cat("\n")
}


Output:

* * * * * * 
* * * * * *
* * * * * *
* * * * * *
  • Initialize n=4, indicating the number of rows in the pattern.
  • The outer loop iterates over the range of i from 1 to n (inclusive), controlling the number of rows in the pattern.
  • Inside the outer loop, the inner loop iterates over the range of j from 1 to the value of i (inclusive). This loop controls the number of asterisks to be printed in each row.
  • Inside the inner loop, cat(“* “) prints an asterisk followed by a space, generating the pattern elements.
  • After the inner loop, cat(“\n”) is used to print a newline character, moving to the next line after printing the pattern for each row.

Example 3: Draw inverted triangle

R




star = c()
i=1
j=5
 
while(i<=5){
for(j in 1:j){
    star = c(star, "*")
}
     
print(star)
     
star = c()
 
i=i+1
j=j-1
}


Output:

[1] "*" "*" "*" "*" "*"
[1] "*" "*" "*" "*"
[1] "*" "*" "*"
[1] "*" "*"
[1] "*"
  • star = c(): Initializes an empty vector star to store the stars for each row.
  • i = 1: Initializes the row counter to 1.
  • j = 5: Initializes the column counter to 5 (this represents the number of stars in the first row).
  • while (i <= 5) { ... }: This is the outer while loop that iterates through 5 rows.
  • for (j in 1:j) { ... }: This is the inner for loop that iterates through the stars within a row.
  • star = c(star, "*"): Adds a star to the current row.
  • star = c(): Clears the star vector to prepare for the next row.
  • i = i + 1: Increments the row counter for the next iteration.
  • j = j - 1: Reduce the number of stars for the next row.