Java starting from beginner, nested / break / continue

Introduction

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.

environment

Language: Java11, JDK13.0.2 Operating environment: Windows 10

Nested

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.

At the end

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?

reference

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

Java starting from beginner, nested / break / continue
Java, instance starting from beginner
Java starting from beginner, inheritance
Java overload constructor starting from beginner
Java starting from beginner, variables and types
Java, if statement / switch statement starting from beginner
Java, for statement / while statement starting from beginner
Java starting from beginner, class declaration / object generation
Java life starting from scratch
Java, abstract classes starting from beginners
Java, interface to start from beginner
java beginner 4
java beginner 3
java beginner
Java starting from beginners, logical operators / conditional operators
Getting Started with Java Starting from 0 Part 1
IntelliJ starting from 1
[Java] Nested class
Java Beginner Exercises
For Java engineers starting Kotlin from now on
Java Exercise "Beginner"
[Java] Platforms to choose from for Java development starting now (2020)
Call Java from JRuby
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Eval Java source from Java
Picture-in-picture starting from iOS14
Access API.AI from Java
From Java to Ruby !!