Java ape small circle nested loop syntax introduced

Java is one of the programs have been used by programmers, but also the most extensive application of a lot of applications will be used to get a small circle java ape lecturer will introduce you to use Java syntax loop nest introduction, I hope you have the help.

Nested loop refers to a loop of the loop re-define a loop statement syntax structure. while, do ... while, for loop statements can be nested, and they can also be nested between one another, such as the most common for loops nested in a for loop, the following format:

for (initialization expression; cycling conditions; operation expression) {
.........
for (initialization expression; cycling conditions; operation expression) {
execute statement
.........
}
.........
}
followed by a practice, implemented using "*" print right triangle, as shown below. ForForDemo.java

{Public class ForForDemo. 1
2 public static void main (String [] args) {
. 3 int I, J; // define two loop variable
4 for (i = 1; i <= 9; i ++) {// outer loop
5 for (j = 1; j <= i; j ++) {// inner loop
. 6 of System.out.print ( " "); // print
. 7}
. 8 of System.out.print ( "\ n-"); / / linefeed
9}
10}
11}
shown results are as follows.

In the above code is defined for the two-loop, respectively, and the outer loop inner loop, the outer loop for controlling the number of lines of printing, for printing the inner loop, " ", each row, " " row number increase, the final output a right triangle. Since the nested loop more complex procedures, the following substeps explain in detail, as follows:

The first step, in line 3 of code defines two loop variables i and j, where i is the outer loop variable, j is the inner loop variable.

The second step, in line 4 of code i is initialized to 1, the condition i <= 9 is true, the first time into the loop of the outer loop.

A third step, the line code 5 j is initialized to 1, since the value of i is 1 at this time, the condition j <= i is true, the first loop into the inner loop, a print "*."

A fourth step of performing line 5 of the inner loop operation code expression j ++, increment the value of j is 2.

A fifth step, performing determination condition code line 5 j <= i, the determination result is false, the end of the inner loop. Behind the implementation of the code, print newline.

A sixth step, performed in the fourth row of the outer loop of code i ++ operative expressions, increment the value of i is 2.

A seventh step, performed in line 4 of code determination condition i <= 9, the determination result is true, the loop enters the outer loop, the inner loop continues.

An eighth step, since the value of i is 2, the inner loop will be performed twice, i.e., two print "*" in the second row. Print newline at the end of the inner loop.

The ninth step, and so on, in the third row will print three "*", the progressive increments, until the value of i is 10, determining the condition of the outer loop i <= 9 evaluates to false, the end of the outer loop , the whole process will be over.

Above Java nested loop syntax introduced remember? There is no knowledge not remember, can not remember not quickly covered up, a small ape circle to remind you not only need to learn Java to view data, video, still need is to practice, a lot of practice, only the knowledge you learned mastery of all, you will be a real, so you can tie into your ideal job, if any problems encountered on the job or can learn to ape small circle to find the answer, I believe you will be very satisfied.

Guess you like

Origin blog.csdn.net/weixin_44867000/article/details/91948287