A story that I finally understood Java for statement as a non-engineer

Preface

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.

meaning

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"

Conditional part of for

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)

Repeated content

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);

Process up to this point in order

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

A story that I finally understood Java for statement as a non-engineer
Java for statement
I made a Diff tool for Java files
[Java] for statement, while statement
I wrote a test code (Junit & mockit) for the code that calls the AWS API (Java)
[Java] for statement / extended for statement
[Java basics] Let's make a triangle with a for statement
(Memo) Java for statement
I tried using an extended for statement in Java
I made a Wrapper that calls KNP from Java
About a double loop that puts a For statement inside a For statement
Java: A story that made me feel uncomfortable when I was taught to compare strings with equals for no reason.
A story that I realized that I had to study as an engineer in the first place
I translated [Clone method for Java arrays] as the Clone method in Java arrays.
A story that I thought I didn't throw away my life
Find a Java library for Bayesian networks that might work
What I pointed out as a reviewer for half a year
I investigated Randoop, a JUnit test class generator for Java
I haven't understood after touching Spring Boot for a month
[Java] Basic statement for beginners
[Eclipse] Flow of introduction as a Java development environment (I investigated various things at that time)
How to deal with the type that I thought about writing a Java program for 2 years
The story of Collectors.groupingBy that I want to keep for posterity
I made a question that can be used for a technical interview
I made a method to ask for Premium Friday (Java 8 version)
Rubocop-daemon as a countermeasure for the problem that RuboCop starts slowly
How to make a groundbreaking diamond using Java for statement wwww
[JDBC] I tried to make SQLite3 database access from Java into a method for each SQL statement.
I created a PDF in Java.
I made a shopify app @java
A really scary (Java anti-pattern) story
[Java] Really scary switch statement story
I met a great switch statement
I tried learning Java with a series that beginners can understand clearly
A story about a Spring Boot project written in Java that supports Kotlin
[Java] How to turn a two-dimensional array with an extended for statement
[Beginner] A story about starting studying Java for job hunting ~ 2nd month ~
[Beginner] A story about starting studying Java for job hunting ~ 3rd month ~
[Beginner] A story about starting studying Java for job hunting ~ 1st month ~
[Beginner] A story about starting studying Java for job hunting ~ 5th month ~
I made a class that can use JUMAN and KNP from Java
[Beginner] A story about starting studying Java for job hunting ~ 4th month ~
I tried to make a program that searches for the target class from the process that is overloaded with Java