Label Break and Continue Statements

zhaozj2021-02-17  79

Break and Continue statements with labels

Border Madman December 2002

There is a goto keyword in Java, but this keyword does not have any effect. In other words, we can't use Goto to jump to a row. In fact, the structured program design does not require a GOTO statement at all, and the use of the GOTO statement often reduces the readability of the program, so Java does not allow GOTO jumps.

The label can also be defined in Java, using the identifier to add a colon (:), such as "MyLabel:". However, since Goto in Java has no substantial role, the label design is of course not for Goto.

The label in Java is designed for recycling, which is designed to use Break and Coutinue in multiple cycles. For this reason, Java's label can only be defined in three cycles (for () {}, do {} while (), while () {} start position, otherwise the compiler will report to find label . The later example can be more intuitive.

In front of the loop, add a label, it seems to have a name to the loop. When you use the Break or Continue statement in the loop, you can bring this tag as a parameter, indicate which cycle of the Break or Continue, such as "Break Mylabel;", "Continue MyLabel;". Now see the example 1. Intuitive understanding of the label usage, where the comments have been slightly explained, what is per-divided code:

Example 1: LabelexMaple.java

/ *

* @file labelexample.java

* @Author James Fan

* /

/ **

* Labelexample class will randomly generate a two-dimensional array,

* The data in each row in arrays is ranked in the order of small to, but there is no sorting between each line.

* At the same time, Labelexample will also randomly find a number as the data to be found from the generated two-dimensional array.

* Subsequently use the tagged Break and Continue statements to optimize the search code.

* /

Public class labelexample {

INT row; // number of lines of two-dimensional array

INT col; // two-dimensional array data number

int [] DATA; / / array data

INT LOOKFOR; / / To find the number in the array

/ **

* Construct the function, generate a number of rows by the ROW, specifying the number of columns by col.

* /

Public Labelexample (int Row, int co) {

THIS.ROW = ROW;

THIS.COL = COL;

Creatematrix ();

}

/ **

* Generate random arrays and randomly extracts to find the number.

* /

Private void creatematrix () {

Data = new int [row] [];

For (int i = 0; i

Data [i] = new int [col];

INT T = 0;

For (int J = 0; j

T = (int) (Math.random () * 20);

Data [i] [j] = t;

}

}

Lookfor = Data [(int) (Math.random () * COL)];

}

/ **

* Print an array content.

* /

Public void printmatrix () {system.out.println ("Row =" row ", col =" col ", lookfor =" ingvent);

For (int i = 0; i

For (int J = 0; j

System.out.print ("/ T" DATA [I] [J]);

}

SYSTEM.OUT.PRINTLN ();

}

}

/ **

* Demo the lookup process, use the tagged BREAK and CONTINUE statements.

* /

Public void search () {

// loop1: // If the label is defined here, it will not be ignored because it is not followed by the loop statement.

/ * The label that is ignored, if used in the Break or Continue statement, can't pass when compiling.

* If it is not used, compiling can be successful.

* /

System.out.println ("--- Begin Searching ---");

LOOP1:

For (int i = 0; i

For (int J = 0; j

IF (Data [i] [j]> lookfor) {

System.out.println ("--- jump ---");

Continue loop1; // Think of this by break, what changes will change

}

IF (data [i] [j] == lookfor) {

System.out.println ("Found: DATA [" i "] [" J "] =" LookFor);

Break loop1; // Think about it with Return, what changes will change

}

System.out.println ("DATA [" i "] [" J "] =" DATA [i] [j]);

}

System.out.println ("--- loop2nd ---");

}

System.out.println ("--- End Searching ---");

}

/ **

* Main program.

* /

Public static void main (String [] args) {

Labelexample test = new labelexample (3, 5);

Test.printmatrix ();

SYSTEM.OUT.PRINTLN ();

Test.search ();

}

}

The results of this program are as follows:

Row = 3, COL = 5, Lookfor = 48

11 21 22 38 39

14 22 40 55 72

11 29 38 48 63

--- Begin Searching ---

Data [0] [0] = 11

Data [0] [1] = 21

Data [0] [2] = 22

Data [0] [3] = 38

Data [0] [4] = 39

--- loop2nd ---

Data [1] [0] = 14Data [1] [1] = 22

Data [1] [2] = 40

--- jump ---

Data [2] [0] = 11

Data [2] [1] = 29

Data [2] [2] = 38

Found: Data [2] [3] = 48

--- End search ----

Since all data in the program are randomly generated, I have been running the program several times before I run the program.

In the above program, the Creatematrix method and the PrintMatrix method are tools, one for initialization data, and the other for printing an array. The other method Search is the key to the demo program.

The data printed in the Search method is enough to explain the procedure of the lookup. The first For loop (that is, the one of the label is LOOP1) is used to traverse all groups (ie, each row) in the 2D array; the second for cycle is nestled in the first For loop, used to traverse each group. All data in order to look up in turn.

If the second For loop does not find the data you want to find during a set of data, there are two cases: 1) The data is all smaller than the data to be found, then this loop can be complete, The statement after the loop can be executed, ie output "--- loop2nd ---". 2) This group of data has data that is larger than the data to be found, then executes Continue Loop1 when checking data larger than the data to be found. This statement not only jumps out of the second For loop, but also aborts the statement that has not yet been executed in the first FOR loop, directly performs the next loop of the first for loop. There is a problem here. What if it changes Continue loop1 to BREAK? If you change, the input result will be:

......

--- jump ---

--- loop2nd ---

......

The reason for this phenomenon is that Braek only stops the second loop, but there is no statement that has not been executed in the first loop.

Now let's see if you find the target data. If you find a target data, no matter which cycle is executed. Therefore, there should be aborted two cycles. Since it is found in the second loop, the second loop is nestled in the first cycle, if direct Break, only the second loop can only continue; Break loop1 specifies the first loop, since the first loop is aborted, then the second loop depending on the first loop is of course aborted. There is also a problem here, why not use Return? I think this problem is better than the previous problem, because if you use Return, what should I do if the statement after the two for loops?

It should be noted that CONTINUE and BREAK here are used to use the loop1 tag. If you are swaping in the location of the code "system.out.println (" - begin search --- ");" and "loop1:", what is the result? At this time, the compiler will report that the Loop1 tab is not found. This is the reason why the loop statement must be followed after the tag definition. However, if there is no BREAK or CONTINUE statement in the program to use loop1, it doesn't matter where loop1 is defined, the compiler will ignore it.

If you need to reprint, please indicate the author and the source.

转载请注明原文地址:https://www.9cbs.com/read-31319.html

New Post(0)