Java 中的嵌套循环是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19996813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:10:00  来源:igfitidea点击:

How do nested loops in Java work?

javanested-loops

提问by JavaLearner

I am new to Java programming and trying to learn the basics of coding. I want to know how this snippet of code works?

我是 Java 编程的新手,正在尝试学习编码的基础知识。我想知道这段代码是如何工作的?

for (int i = 1; i <= 5; i++) {
    for (int j = 1; j <= 10; j++) {
        System.out.print((i * j) + " ");
    }
    System.out.println();
}

I will be really thankful if the programming sherlocks present here can explain me the logic.

如果这里的编程夏洛克能解释我的逻辑,我将非常感激。

采纳答案by david99world

It will do...

它会做...

1*1 1*2 1*3 till it gets to 1*10, then on a new line
2*1 2*2 2*3 and it will go to all the way to 
.
.
5*10

So it will print out 1 2 3 4 5 ...till 10, then do a new line. Output below.

所以它会打印1 2 3 4 5 ...到 10,然后换行。下面输出。

1 2 3 4 5 6 7 8 9 10 
2 4 6 8 10 12 14 16 18 20 
3 6 9 12 15 18 21 24 27 30 
4 8 12 16 20 24 28 32 36 40 
5 10 15 20 25 30 35 40 45 50 

回答by Alexander Cogneau

So you'll work from te outside to the inside: every time the 'top' loop will run, that is, 5 times, it will execute the code between the brackets.

因此,您将从外到内工作:每次“top”循环运行时,即 5 次,它都会执行括号内的代码。

In there, there is another loop, using j as index, which will run 10 times. So when we run the upper loop 5 times, and time it executes another loop 10 times, we'll get that this run 50 times. So 50 times, this will print out the product of i and j.

在那里,还有另一个循环,使用 j 作为索引,它将运行 10 次。因此,当我们运行上层循环 5 次,并且它执行另一个循环 10 次时,我们将得到这次运行 50 次。所以 50 次,这将打印出 i 和 j 的乘积。

After each 10 loops of the 'j'-loop, it will print out a new line.

在'j'-loop 的每 10 次循环之后,它将打印出一个新行。

回答by Thirumalai Parthasarathi

the inner loop will be executed 10 times for each iteration of the outer loop so this inner loop will get executed for 50 times in this program.

对于外循环的每次迭代,内循环将执行 10 次,因此该内循环将在此程序中执行 50 次。

when i =1the control enters the outer loop and control flows to the inner loop, now j=1and it satisfies the condition j<=10so will enter the inner loop

i =1控制进入外循环,控制流向内循环时,现在j=1满足条件j<=10所以进入内循环

now this will print

现在这将打印

1

then jwill get incremented and jwill be 2 still satisfying the condition j<=10and prints

thenj将增加并且j仍为 2 仍然满足条件j<=10并打印

2 

now the line looks like

现在这条线看起来像

1 2

this goes on till j<=10is false that is when jis 11the inner loop will stop executing and control flows to the next print statement that prints a new line

任其发展下去,直到j<=10是假的也就是当j11内循环将停止执行,并控制流向,打印一个新行的下一个打印语句

then igets incremented to 2and the same thing happens till i <=5is false that is when ibecomes 6the loop stops.

然后i被递增到2,同样的事情发生,直到i <=5是假的就是当 i成为6循环停止。

回答by Paul Samsotha

for (int i = 1; i <= 5; i++) {   // outer loop iterates 5 times.
    for (int j = 1; j <= 10; j++) {  // for each iteration of outer loop, 
                                     // inner loop iterates 10 times
        System.out.print((i * j) + " ");
    }
    System.out.println();
}

First iteration of outer loop (10 iterations of inner loop)

外循环的第一次迭代(内循环的 10 次迭代)

i = 1, j = 1
i = 1, j = 2
...
i = 1, j = 10

Second iteration of outer loop (10 iterations of inner loop)

外循环的第二次迭代(内循环的 10 次迭代)

i = 2, j = 1
i = 2, j = 2
...
i = 2, j = 10

...

...

Last iteration of outer loop (10 iterations of inner loop)

外循环的最后一次迭代(内循环的 10 次迭代)

i = 5, j = 1
i = 5, j = 2
...
i = 5, j = 10

回答by CHowdappaM

because of trying to learn the basics of coding, sharing this.

因为trying to learn the basics of coding,分享这个。

Once you come inside the loop (i), you faced second loop (j).

一旦您进入循环 (i),您将面临第二个循环 (j)。

Now second loop will finish first, so for each i, j will be 1-10.

现在第二个循环将首先完成,因此对于每个 i,j 将是 1-10。