递归循环(C#) [英] Recursive loop (C#)

查看:234
本文介绍了递归循环(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释吗?我写了一个函数来计算这样一个数字在C#中的阶乘:

Can someone please explain it to me? I wrote a function to calculate the factorial of a number like this in C#:

public int factorial(int input)
{
    if (input == 0 || input == 1)
        return 1;
else
{
    int temp = 1;
    for (int i = 1; i <= input; i++)
        temp = temp * i;
    return temp;
    }
}

但我发现了一些C ++代码真的知道任何C ++ btw)使用递归循环找到一个阶乘:

But I found some C++ code (I don't really know any C++ btw) which finds a factorial using a recursive loop:

int factorial(int number) {
 int temp;
 if(number <= 1) return 1;
 temp = number * factorial(number - 1);
 return temp;
}

有人可以向我解释它是如何工作的?感谢。

Can someone explain to me how it works? Thanks.

推荐答案

在语法上,C ++代码与用C#编写的代码完全相同。不要让语言差异抓住你!它实际上看起来像C给我,假定变量被声明在函数的顶部;这在C ++或C#中不是绝对必要的。我喜欢在第一次使用它们时声明变量,将声明和初始化结合在一个语句中,但这只是一种不改变代码功能的样式偏好。

Syntactically, the C++ code is identical to the same code written in C#. Don't let the language discrepancy catch you off guard! It actually looks like C to me, given that the variable is declared at the top of the function; that's not strictly necessary in either C++ or C#. I prefer to declare variables the first time I use them, combining the declaration and initialization in one single statement, but that's merely a stylistic preference that doesn't change the function of the code.

我将尝试通过向代码段的每一行添加注释来解释这一点:

I'll try to explain this by adding comments to each line of the code snippet:

// Declare a function named "Factorial" that accepts a single integer parameter,
// and returns an integer value.
int Factorial(int number)
{
    // Declare a temporary variable of type integer
    int temp;

    // This is a guard clause that returns from the function immediately
    // if the value of the argument is less than or equal to 1.
    // In that case, it simply returns a value of 1.
    // (This is important to prevent the function from recursively calling itself
    // forever, producing an infinite loop!)
    if(number <= 1) return 1;

    // Set the value of the temp variable equal to the value of the argument
    // multiplied by a recursive call to the Factorial function
    temp = number * Factorial(number - 1);

    // Return the value of the temporary variable
   return temp;
}

递归调用仅仅意味着该函数从同一个函数内调用自身。这是因为n的阶乘相当于以下语句:

Recursive calls simply mean that the function calls itself from within the same function. This works because the factorial of n is equivalent to the following statement:

n! = n * (n-1)! 

一个伟大的方式来了解代码的工作原理是将它添加到测试项目,然后使用调试器单步执行代码。 Visual Studio在C#应用程序中有非常丰富的支持。您可以观察函数如何递归调用自身,观察每行执行顺序,甚至看到变量的值随着对它们执行操作而改变。

One great way to understand how code works is to add it to a test project, then single-step through the code using the debugger. Visual Studio has very rich support for this in C# applications. You can watch how the function recursively calls itself, watching each line execute in sequence, and even seeing the values of the variables change as operations are performed on them.

这篇关于递归循环(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆