Do you guys get along with the for
statement? I was in a fight until yesterday. I didn't know what the for
statement was thinking about, running around in a row and a row, going over and over. However, when I was trying to solve the JAVA question that caught my eye by trial and error, a ray of light came in and the for
sentence said:
"Let's be friends".
First, let's review the basics of the for
statement. The for
statement consists of four parts: initialization, condition, change, and execution content </ b>.
for (Initialization;conditions;change) {
Execution content
}
for
statement is started, it is executed only once at the beginning </ b>. Don't forget to declare the type of the variablefalse
. In other words, the execution will be repeated as long as the conditions are met </ b>for (int i = 0; i < 10; i++) {
System.out.println("Hello!");
}
int
i = 0
0
for the variable ʻi`i < 10
is less than
10, it is
true. In other words, the execution content is repeated until ʻi
is 9
.i++
System.out.println("Hello!")
Hello!
Let's see it based on the demonstration. Initialization is the first thing that works when the for
statement is started. The value of the variable ʻi is
0. The
for statement confirms that ʻi
is 0
and then moves on to the condition. The condition is ʻi <10. At this point it is the same as
0 <10. When translated into Japanese, it can be said that "0 is less than 10", so the condition is
true. Since it was confirmed that the condition is
true`, the part of the execution content moves.
When System.out.println ("Hello! ")
Is executed, the next step is change. The change is ʻi ++ . At this point, it's
0 ++, so it's now
1. This
1moves to the condition to assign to the variable of execution. At this point it is
1 <10. It's
true`. That's why I will move on to the execution contents again.
If this flow continues many times, the condition will be 10 <10
. Is this true
? Is it false
? It's false
. This concludes the for
statement. Thank you for your hard work.
The flow is listed below.
for
statementfor
statement if condition is false
Now that we have the basics of the for
statement, the next step is nesting. I thought I somehow understood the nesting of the for
statement, but when I actually wrote it, I didn't understand how the upper for
statement and the lower for
were entwined. did.
Let's see the movement of nesting while unraveling the multiplication table.
public class MultiplicationTable {
public void multiply() {
for (int x = 1; x < 10; x++) {
for (int y = 1; y < 10; y++) {
System.out.print(x * y);
System.out.print(" ");
}
System.out.println();
}
}
}
From this point onward, the expression is "outer for
statement "instead of" upper for
statement", and "inner for
statement" instead of "lower for
statement". Then, from the third line. Executed from ʻint x = 1; . Then
x <10;. This time it is
true`, so let's move on to the execution content. If you are a beginner, it will be blank to see where the execution is, but please see the figure below.
The for
statement has been enlarged and color coded. As you can see from this, the execution content of the outer for
statement is the inner blue for
statement. The initial value of the inner for
statement (blue), ʻint y = 1, is executed to move to the condition. Since
y <10;, substitute it and it is
1 <10. Since it is
true, it moves to the execution content. It is
System.out.print (x * y).
x was a variable in the outer
forstatement. At this point,
x = 1. So here
1 * 1 is executed and the result is
1.
1is displayed on the screen and moves to the next line.
System.out.print ("") `displays a half-width space.
Once this is done, the next step is to change. Since it is y ++
, y
increases by one and changes to 2
, and then the condition moves on. Since it is y <10
, it becomes 2 <10
and it is true
, so it moves to the execution contents again. It is System.out.print (x * y)
. Since it is 1 * 2
, the result will be 2
and will be displayed on the screen, and a half-width space on the next line will also be displayed. When the value of y
reaches 9
by such a movement, 1 2 3 4 5 6 7 8 9
is displayed on the screen. Let's see what happens after this.
With y ++
, y
becomes 10
and the condition moves. Since 10 <10
is false
, System.out.println ()
at the bottom of the inner for
statement (blue) is executed to start a new line. This ends the inner for
statement (blue) and returns to the outer for
statement (red). From here on, it's repeated. If you start with 2 * 1
and go to 2 * 9
, 3 * 1
will be executed with a line break. Let's see the actual execution result.
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81
This is the behavior of the multiplication table program using the nesting of for
statements. It's easy to unravel. Applying this, I made a question that I asked at the beginning. The result is here.
■
■■
■■■
■■■■
■■■■■
■■■■■■
■■■■■■■
■■■■■■■■
■■■■■■■■■
■■■■■■■■■■
■■■■■■■■■■
■■■■■■■■■
■■■■■■■■
■■■■■■■
■■■■■■
■■■■■
■■■■
■■■
■■
■
What I'm doing is using the for
statement to increase the number of■
by line to express a triangle. I made a shape that increases by one and a shape that decreases by one, and displayed the top and bottom combined. Below is the program.
public class TriangleTop {
public void traiangle() {
for (int x = 0; x < 10; x++) {
for (int y = 0; y < x+1; y++) {
System.out.print("■");
}
System.out.println();
}
}
}
public class TriangleBottom {
public void traiangleReverse() {
for (int x = 0; x < 10; x++) {
for (int y = 10; y > x; y--) {
System.out.print("■");
}
System.out.println();
}
}
}
If you've read this far, you don't have to explain it anymore.
It is difficult for programming beginners to immediately understand the behavior of nesting for
statements. It is recommended that you do not just look at it, but first try and error with your own hands. I wanted to make a triangle that was flipped to the left, so I tried a quadrangle, but I gave up because I couldn't display half-width spaces continuously. Someday I would like to draw a simple picture with the for
statement.
Also, is it okay for Qiita to post something like this?
Recommended Posts