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
Have you ever seen a matryoshka doll? It is a stack of many similar objects of different sizes. The nesting dealt with here is ** nesting of control structures **.
ʻIf
for`` while` statement. The control structure is also included in the processing, and it is suitable for performing hierarchical processing such as double loop and triple loop.
break
The break
itself was mentioned in the switch
statement, but if you explain it after nesting, ** "interrupt the nesting that you are currently processing and enter the upper loop" ** .. Even if you are in an infinite loop, you can exit it if the conditions of the break statement are met.
The following is a quote from the reference ** Kuromoto **
int num = 0; while(true){ num++; if(num == 3){ break; } System.out.println(num); }
continue
`continue` is often mistaken for` break`, but if ** `continue` is done, the iterative process below` continue` will be skipped entirely and the sentence will move to the update statement **.
An example of continue will be added later. None of the code I came up with worked with a compilation error. Especially practice is required.
# Practice
Let's look at an example of failure.
#### **`nestingTwo.java`**
```java
class nestingTwo
{
public static void main(String[] args)
{
int forLoop = 0;
int whileLoop = 0;
//The total number should be 9
for(; forLoop < 3; forLoop++){
while(whileLoop < 3){
whileLoop++;
}
}
System.out.println("The process is finished.");
System.out.println("The final result is a for statement" + forLoop + "Time, While statement" + whileLoop + "It was a time.");
}
}
/*The process is finished.
The final result was 3 for statements and 3 While statements, for a total of 3 times.*/
whileLoop
satisfies the condition once it becomes 3 in the nesting of for, and the for has only been turned once and no changes have been made since then.
This time, in order to make the number of for x the number of while.
after
(abridgement)
int totalCounter = 0;
//The total number should be 9
int whileLoopTemporary =0;
for(; forLoop < 3; forLoop++){
while(whileLoop < 3){
whileLoop++;
totalCounter++;
whileLoopTemporary = whileLoop;
}
whileLoop = 0;
}
System.out.println("The process is finished.");
System.out.println("The final result is a for statement" + forLoop + "Time, While statement" + whileLoopTemporary + "Times, in total" + totalCounter + "It was times.");
}
}
A whileLoopTemporary
that releases the accumulated rotation speed in the while is created and substituted, and later initialized to 0
in the for statement.
There must be a smarter way.
There seems to be a mechanism called a label that transfers control to ** labeled processing **, but it is omitted here. Is it the same as the go to
I saw somewhere?
I write variables and expressions as much as possible and compile them, so if I want to quote them completely, I will describe that.
Easy Java 7th Edition Java SE11 Silver Problem Collection (commonly known as Kuromoto)
Recommended Posts