This article is a memorandum. Although it is a reference book level content, the code posted in this article is about ** Wrong ** are the main things. This is for the purpose of posting the part that was actually mistaken during coding and posting it for self-reflection. In addition, I will not touch on the deep part here because I will review it later while also studying the Java Silver exam questions.
Language: Java11, JDK13.0.2 Operating environment: Windows 10
The ** for statement ** is one of the iterative processing statements for ** iterative processing ** until the loop is exited, based on the ** conditional statement **. It is suitable for repeating by specifying the number of times.
Basic form of for statement
for (Initialization statement; Conditional statement; Update statement){
//Iterative processing;
}
Click here for each role.
amount10Years.java
double amount = 100000;
for (int year = 1; year < 10; year++){
amount *= 1.02;
}
System.out.println("10 years later" + amount + "It becomes a circle.");
//Something is missing ...
In this case, the first year is the second year, and only nine compound interest calculations can be made. If you make a mistake in setting the initial value, it will affect the number of times.
Along with the for statement, it is a statement that repeats processing. It is suitable for repeating until the expected condition is reached, but be careful because it is easier to create ** infinite loop ** than for statement.
Basic form of while statement
while (Conditional statement){
//Iterative processing;
}
For the conditional statement, see the one in the explanation of the for statement.
As another form, ** do ~ while statement ** also exists. Since the process is repeated at least once, it is recommended to use it when the essential process is included. Pay close attention to the position of the semicolon ;
.
do~Basic form of while statement
do{
//Iterative processing;
} while (Conditional statement);
whileAmount150k
double amount = 100000;
int year = 0;
//This time I properly set the first year to 0
while (amount <= 150000) {
amount *= 1.02;
}
System.out.println(year + "It will exceed 150,000 yen over the years.");
//The target amount was reached in less than a year ...
This time, I didn't include year ++
in the processing of the while statement, so the final output became strange.
It's a very useful sentence, but it also carries the risk of ridiculous results if left misunderstood. I want to understand it well because it is used in areas such as interest rate calculation and algorithms that can never be mistaken.
For this reference, I put a Java qualification test book, but it doesn't have {}
(curly braces), the nesting structure is deep and it takes time to understand, increment
・ decrement
Is spread in large quantities. There are good problems that require both comprehension and concentration.
I write variables and expressions as much as possible and compile them, so if I want to quote them completely, I will describe that.
Java SE11 Silver Problem Collection (commonly known as Kuromoto)
Recommended Posts