📜  如何在 R 中创建嵌套的 For 循环?

📅  最后修改于: 2022-05-13 01:54:46.789000             🧑  作者: Mango

如何在 R 中创建嵌套的 For 循环?

编程语言中的循环是一系列指令一个接一个地执行,除非满足最终条件。在程序中使用循环非常频繁。

需要一个循环

让我们考虑一个场景,我们想要打印从 1 到 3 的自然数。我们可以简单地一个一个地打印它们。但是假设范围很大,从 1 到 100 或 1 到 10000 甚至 1 到 100000,那么逐个打印它们是一项繁琐的任务。在这种情况下,循环非常有效,并且还提高了源代码的可读性。在 R 中,循环大致分为三类:for、while 和 repeat。本文重点介绍 R 中 For 循环的工作原理。

R中的for循环:

For循环是常用的循环类型之一。它是一种控制语句,用于需要执行多个语句的场景。在 R 中,for 循环遵循以下语法。

句法:

在这里,一个元素是序列中的实际值,一个接一个,从头开始。

R中的嵌套for循环:

嵌套的 for 循环在另一个 for 循环内有一个 for 循环。对于外部 for 循环中的每次迭代,将执行内部循环,除非内部循环满足最终条件。一旦为特定的外部迭代执行了内部 for 循环,则外部 for 循环将进行下一次迭代,现在将为该迭代执行内部循环。这个过程不断重复,直到满足外部 for 循环的最终条件。嵌套的 for 循环可以可视化为在二维空间中一一迭代整数坐标。例如,印刷

(0,0)   (0,1)   (0,2)
(1,0)   (1,1)   (1,2)
(2,0)   (2,1)   (2,2) 
(3,0)   (3,1)   (3,2) 

下面是 R 中嵌套 for 循环的语法。

句法:

示例 1:在下面的程序中,我们在外部循环中迭代 0 到 3,对于外部 for 循环中的每个值,我们在内部 for 循环中迭代 0 到 3。

R
# R program to illustrate the working of
# nested for-loop 
  
# Iterating over 0 to 3 in the outer
# for-loop
for (x in 0:3){
        
      # Iterating over 0 to 3 in the inner 
    # for-loop
      for (y in 0:3)
    {
        # Print x and y 
          print(paste("(", x, ",", y, ")"))
  
    }
}


R
# R program to illustrate the working 
# of nested for-loop
  
# Iterating over 1 to 5 in the outer 
# for-loop
for(number1 in 1:5)
{
      # Iterating over 1 to 5 in the inner 
    # for-loop
    for(number2 in 1:5)
    {
        # Print the sum of number1 and number2
        print(paste(number1, "+", number2, "=", 
                    number1 + number2));
    }
}


R
# R program to illustrate the working
# of next statement in nested for-loop
  
# Iterating over outer loop
for(number1 in 1:5)
{
      if(number1 == 1)
      next
      # Iterating over outer loop
    for(number2 in 1:5)
    {
        # Print the sum of number1 and number2
        print(paste(number1, "+", number2, "=",
                    number1 + number2));
    }
}


R
# R program to illustrate the working
# of next statement in nested for-loop
  
# Iterating over outer loop
for(number1 in 1:5)
{
      # Iterating over outer loop
    for(number2 in 1:5)
    {
        # If number1 is equal to 1
        # Then skip the print statement below
        # and move to the next iteration
        if(number2 == 1)
              next
        
        # Print the sum of number1 and number2
        print(paste(number1, "+", number2, "=",
                    number1 + number2));
    }
}


R
# R program to illustrate the working
# of break statement in nested for-loop
  
# Iterating over 1 to 5 in outer for-loop
for(number1 in 1:5)
{
    # If number1 is equal to 2
    # then break the flow of control
    # and come out of the outer loop
      if(number1 == 2)
      break
        
    # Iterating over 1 to 5 in inner for-loop
    for(number2 in 1:5)
    {
        # Print the sum of number1 and number2
        print(paste(number1, "+", number2, "=", 
                    number1 + number2));
    }
}


R
# R program to illustrate the working of
# break statement in nested for-loop
  
# Iterating over 1 to 5 in the outer for-loop
for(number1 in 1:5)
{
  
# Iterating over 1 to 5 in the inner for-loop
    for(number2 in 1:5)
    {
        # If number2 is equal to 2 then stop
        # the flow of control
        # and come out of the inner for-loop at the moment
        if(number2 == 2)
            break
              
          # Print the sum of number1 and number2
        print(paste(number1, "+", number2, "=", 
                    number1 + number2));
    }
}


输出:

示例 2:在下面的程序中,我们在外部循环中迭代 1 到 5,对于外部 for 循环中的每个值,我们在内部 for 循环中迭代 1 到 5 并同时打印总和这两个值。

R

# R program to illustrate the working 
# of nested for-loop
  
# Iterating over 1 to 5 in the outer 
# for-loop
for(number1 in 1:5)
{
      # Iterating over 1 to 5 in the inner 
    # for-loop
    for(number2 in 1:5)
    {
        # Print the sum of number1 and number2
        print(paste(number1, "+", number2, "=", 
                    number1 + number2));
    }
}

输出:

下一个声明:

大多数语言都提供了一些功能,我们可以使用这些功能跳过当前的迭代,如果存在则进行下一个迭代。为此,我们在 R 中有“下一个”语句。

示例 1:在下面的程序中,如果number1的值等于 1,我们将跳过内部 for 循环。

R

# R program to illustrate the working
# of next statement in nested for-loop
  
# Iterating over outer loop
for(number1 in 1:5)
{
      if(number1 == 1)
      next
      # Iterating over outer loop
    for(number2 in 1:5)
    {
        # Print the sum of number1 and number2
        print(paste(number1, "+", number2, "=",
                    number1 + number2));
    }
}

输出:

示例 2:在下面的程序中,如果number2的值在内部 for 循环中等于 1,我们将跳过 print 语句。

R

# R program to illustrate the working
# of next statement in nested for-loop
  
# Iterating over outer loop
for(number1 in 1:5)
{
      # Iterating over outer loop
    for(number2 in 1:5)
    {
        # If number1 is equal to 1
        # Then skip the print statement below
        # and move to the next iteration
        if(number2 == 1)
              next
        
        # Print the sum of number1 and number2
        print(paste(number1, "+", number2, "=",
                    number1 + number2));
    }
} 

输出:

中断声明:

大多数语言都提供了功能,我们可以使用这些功能暂时停止 for-loop 的迭代并退出当前的 for-loop 范围。为此,我们在 R 中使用了“break”语句。R 中的 Break 语句与 C 或 C++ 中的 break 语句相同。

示例 1:在下面的程序中,一旦number1的值接近 2,外部 for 循环的控制流就会超出范围。

R

# R program to illustrate the working
# of break statement in nested for-loop
  
# Iterating over 1 to 5 in outer for-loop
for(number1 in 1:5)
{
    # If number1 is equal to 2
    # then break the flow of control
    # and come out of the outer loop
      if(number1 == 2)
      break
        
    # Iterating over 1 to 5 in inner for-loop
    for(number2 in 1:5)
    {
        # Print the sum of number1 and number2
        print(paste(number1, "+", number2, "=", 
                    number1 + number2));
    }
}

输出:

示例 2:在下面的程序中,一旦number2的值接近 2,内部 for 循环的控制流程就会超出范围。

R

# R program to illustrate the working of
# break statement in nested for-loop
  
# Iterating over 1 to 5 in the outer for-loop
for(number1 in 1:5)
{
  
# Iterating over 1 to 5 in the inner for-loop
    for(number2 in 1:5)
    {
        # If number2 is equal to 2 then stop
        # the flow of control
        # and come out of the inner for-loop at the moment
        if(number2 == 2)
            break
              
          # Print the sum of number1 and number2
        print(paste(number1, "+", number2, "=", 
                    number1 + number2));
    }
}

输出: