了解嵌套的forloops [英] Understanding nested forloops

查看:82
本文介绍了了解嵌套的forloops的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想更好地理解嵌套循环。



说我有一个for循环时间,这个实例是一个父循环,如下所示:



Hi,

I am trying to better understand nested loops.

Say I have a for loop for time, this instance a parent loop as follows:

int i;
int j;

for (int i = 0; i <=99; i++)
{
  for (j = 0; i <=99; j++)
  {
    blahh...
  }
}





在这个例子中这是否意味着对于变量i的每次重复,变量j都会被执行?



有点令人困惑。



干杯

Keil



In this instance does this mean that for every repition of variable i, variable j gets executed?

A tad confusing.

Cheers
Keil

推荐答案

内循环中的所有步骤都会在外循环的每次执行时执行。
All the steps in the inner loop gets executed for every single execution of the outer loop.


Quote:

在这种情况下,这是否意味着对于变量i的每次重复,变量j都会被执行?

In this instance does this mean that for every repition of variable i, variable j gets executed?

是的,你是对的。理想情况下应该发生。



对于每一个i,内循环应该持续到j为99.



但你有问题这里。内循环检查条件 i< = 99 ,这是错误的。你应该检查 j< = 99

Yes, you are right. Ideally it should happen.

For every i, inner loop should continue till j is 99.

But you have a problem here. The inner loop checks the condition i<=99, which is wrong. You should check j<=99.

for (j = 0; i <=99; j++)


如下所示添加控制台写行,您可以看到每个迭代值的变化方式



add console write line as below, you can see the how each iteration values are changing

for (int i = 0; i <=99; i++)
{
  for (j = 0; j <=99; j++)
  {
    Console.WriteLine(string.Format("i={0}, j={1}", i,j));
  }
}


这篇关于了解嵌套的forloops的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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