This is my first post, but I was happy to understand it, so I would like to share about the loop processing of Java's super-basic for statement.
Before I was 30 years old, I jumped into the IT industry from another genre of sales, It was good until I jumped in, but I was drowning, so I am studying again.
Well, the main subject at once.
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
System.out.println(sum);
}
}
this.
Ah, it's processed repeatedly from 1 to 10. That's a thin knowledge.
But I don't know what order they are working. So, I would like to introduce the flow for non-engineers.
First this part
public static void main(String[] args) {
}
It's like a signal to start when you write a Java program. {} It will do the processing inside the box.
So what makes sense
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
System.out.println(sum);
}
It is no exaggeration to say only here.
And from here.
In a sense, I think programming languages are foreign languages, so I will roughly translate them into Japanese.
int sum = 0;
"The total value of integers is 0"
for (int i = 1; i <= 10; i++) {
}
It ’s like an idiom here for (Initialization; Repeat condition; Update process) { Content to repeat } It seems, but I'm not sure.
My current understanding is as follows. (The integer i is 1; if i is 10 or less, repeat; by the way, add 1 to each number)
sum += i;
Add i to the total value in order
System.out.println(sum);
This is a statement that you can put out on the console (total value);
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
System.out.println(sum);
}
}
1st processing Total value is 0 Mr. i starts from 1 0 + 1 In other words, the total value is 1.
Second process The total value became 1 Mr. i increases by 1 so 2 1 + 2 In other words, the total value will be 3
3rd processing The total value became 3 Mr. i increases by 1, so 3 3 + 3 or 6
Repeat the process like this
On the console 1 3 6 10 15 21 28 36 45 55
It comes out like.
end.
We are looking forward to Masakari. I hope it helps you understand even a little.
Recommended Posts