简体   繁体   English

嵌套for循环如何在C#中工作

[英]How do nested for loops work in c#

I'm not sure how this code is compiled (using visual studio code just fyi) 我不确定如何编译此代码(仅使用Visual Studio代码即可)

int i,j;

for(i=0; i<=6; i++) {

    for (j=1; j<=7-i; j++) {

        Console.Write("*");

    }
    Console.Write("\n"); 
 }

My question is when the code is beginning to get compiled, the first for loop is tested and it will be true, then the nested for loop is then tested and it will be true so "Console.Write(" * "); is written. But then from here I do not understand what happens or why it happens. Does the compiler then run Console.Write(" \\n "); or... 我的问题是,当代码开始被编译时,首先测试for循环 ,它将是true,然后测试嵌套的for循环 ,它将是true,因此编写了“ Console.Write(” *“); 。但是从这里我不知道会发生什么或为什么发生。编译器然后运行Console.Write(“ \\ n”);或...

If you don't understand a for-loop you can always reduce it to simpler statements: 如果您不了解for循环,则可以随时将其简化为更简单的语句:

for ( init ; condition; increment )
    body;

is just a shorter way of writing 只是一种较短的写作方式

init;
while (condition)
{
    body;
    increment;
}

So your loops 所以你的循环

for(i=0; i<=6; i++) {
  for (j=1; j<=7-i; j++) {
    Console.Write("*");
  }
  Console.Write("\n"); 
}

is just a short way of writing 只是一种简短的写作方式

i = 0;
while (i <= 6)
{
  j = 1;
  while (j <= 7 - i)
  {
    Console.Write("*");
    j++;
  }
  Console.Write("\n");
  i++;
}

Is that more clear? 这更清楚吗?

If that's still not clear, you can lower it further. 如果仍然不清楚,您可以进一步降低它。

while(condition)
  body

is just a short way to write 只是写的短途

START:
if (!condition)
  goto END;
body;
goto START;
END:

So your loops are just: 所以你的循环就是:

i = 0;
START_1:
if (!(i <= 6)) 
  goto END_1;
j = 1;
START_2:
if (!(j <= 7 - i)) 
  goto END_2;
Console.Write("*");
j++;
goto START_2;
END_2:
Console.Write("\n");
i++;
goto START_1;
END_1:

Is that now clear? 现在清楚了吗?

variables change like this. 变量会像这样变化。

i = 0 : j changes from 0 to 7 ( 7- i, but i = 0)
i = 1 : j changes from 0 to 6 (7 - i, i = 1)
.
.
.
.
i = 6: j changes from 1 to 1 (7 - i, i = 6)

in each i-loop you're printing j-loop and a new line character. 在每个i循环中,您正在打印j循环和换行符。

|j-loop|i-loop|
|******|'\n'  |

So, you will get the output, 因此,您将获得输出,

******* 
****** 
*****
****
***
**
*

I will not show you the output simply because your question is not what the output is but how the for loop works. 我不会仅向您显示输出,因为您的问题不是输出是什么,而是for循环的工作方式。

Let's do this step by step. 让我们一步一步地做。 Here is some pseudo code: 这是一些伪代码:

for ( init; condition; increment )
{
      Console.Write("*");
}
  1. The init is executed first and it is only done once when the loop is encountered. 首先执行init并且在遇到循环时仅执行一次。 That's right only once for the for loop. 对于for循环,这只对一次。
  2. The condition is evaluated. condition被评估。 If it is true the body is executed. 如果为真,则执行主体。 So Console.Write("*"); 因此, Console.Write("*"); is executed. 被执行。
  3. The increment is executed. 执行increment
  4. The condition is evaluated again. 再次评估condition If it is true the body is executed. 如果为真,则执行主体。 So Console.Write("*"); 因此, Console.Write("*"); is executed. 被执行。

Steps 3 and 4 are done until the condition is false. 完成步骤3和4,直到condition为假。 Once it is false the loop exits and the following line of code is executed. 一旦为假,则循环退出,并执行以下代码行。

In your question you have a loop and if the condition is true it starts another loop. 在您的问题中,您有一个循环,如果条件为真,它将开始另一个循环。 This loop starts at 1 and if i is 0, it is subtracted from 7, 7 - 0 is 7, and since j is 0 and smaller than 7, it performs the body and prints a *. 该循环从1开始,如果i为0,则从7中减去它,7-0为7,由于j为0且小于7,它将执行主体并打印*。 It keeps doing steps 3 and 4 and prints a * seven times. 它继续执行步骤3和4,并打印*七次。 Then the condition is false so it jumps outside the inner loop and prints a new line. 然后条件为假,因此它跳到内循环之外并打印新行。

Then step 3 and 4 are executed for the outer loop. 然后对外部循环执行步骤3和4。

It keeps doing that until the condition in the outer loop evaluates to false. 它一直这样做,直到外部循环中的条件评估为false为止。

i: 0 , j: 1 to 7 print * , then print \\n i: 0j: 1 to 7打印* ,然后打印\\n

i: 1 , j: 1 to 6 print * , then print \\n and so on and on i: 1j: 1 to 6打印* ,然后打印\\n ,依此类推

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM