Java programming things 38-break and continue statements

Source: Internet
Author: User

Java programming those things 38-break and continue statement Zhengzhou game college Chen yuefeng from: http://blog.csdn.net/mailbomb 5.5 break and continue statementsThe break and continue statements are closely related to loop statements. The break keyword indicates the interruption or interruption, and the continue keyword indicates the continuation. You can use these two keywords to adjust the execution of the loop. 5.5.1 break statementThe break statement has been introduced in the previous switch statement to interrupt the execution of the switch statement. In the loop statement, the break statement is used to interrupt the loop statement, that is, to end the execution of the loop statement. The break statement can be used inside three loop statements with identical functions. The following uses the while statement as an example to describe the basic usage and functions of the break statement. Sample Code: int I = 0; while (I <10) {I ++; if (I = 5) {break ;}} then, when the value of variable I is 5, the condition is met, then the break statement is executed to end the entire loop, and the subsequent code of the loop is executed. In a loop statement, you can use the break statement to interrupt the loop being executed. In actual code, the structure is often nested by loop statements because the logic is complex. If a break statement appears inside the nested loop, then, only the loop where the break statement is located is ended. The example code is as follows: for (INT I = 0; I <10; I ++) {for (Int J = 0; j <5; j ++) {system. out. println (j); If (j = 3) {break ;}} then the break statement appears in the loop where the loop variable is J and is executed to the break statement, only the loop with the cyclic variable J is interrupted, but the loop with the cyclic variable I is not affected. In the preceding sample code, if an External Loop needs to be interrupted, you can use the tag statement provided by the syntax to identify the position of the loop and then jump out of the loop corresponding to the tag. The sample code is as follows: lable1: For (INT I = 0; I <10; I ++) {for (Int J = 0; j <5; j ++) {system. out. println (j); If (j = 3) {break label1 ;}}note: label1 is the name of the tag, which can be any legal identifier in Java, the label statement must match the loop. It is written on the corresponding loop statement when used, and the label statement ends with a colon. If you need to interrupt the loop corresponding to the tag statement, use the break followed by the tag name to interrupt the corresponding loop. In the example code, the cycle variable of the break statement is I. The same function can also be implemented using the following logic: Boolean B = false; For (INT I = 0; I <10; I ++) {for (Int J = 0; j <5; j ++) {system. out. println (j); If (j = 3) {B = true; break ;}} if (B) {break ;}} in the sample code, by combining two breaks and one identity variable, the External Loop Structure is exceeded. 5.5.2 continue statementThe continue statement can only be used within the loop statement. The function is to skip this loop and continue to execute the next loop structure. In the while and do-while statements, the continue statement jumps to the cyclic condition and continues to be executed. In the for statement, the continue statement jumps to the iteration statement and continues to be executed. The following uses the while statement as an example to describe the function of the continue statement. The sample code is as follows: int I = 0; while (I <4) {I ++; if (I = 2) {continue;} system. out. println (I);} then the code execution result is: 1 3 4 when the variable I value is equal to 2, execute the continue statement, then, the unfinished loop body will be skipped and directly enter the next loop. In actual code, you can use the continue statement to skip some content in the loop. Similar to the break statement described earlier, the continue statement only skips the structure of the loop when nested loops exist. to skip an External Loop, the tag statement must be used to identify the corresponding loop structure. The sample code is as follows: lable1: For (INT I = 0; I <10; I ++) {for (Int J = 0; j <5; j ++) {system. out. println (j); If (j = 3) {continue label1 ;}}so that when the continue statement is executed, it is no longer a jump to the J ++ statement, instead, it directly jumps to the I ++ statement. 5.5.3 SummaryIn actual code, you can use the break and continue statements as needed to adjust the execution of loop statements. The function of the break statement is to end the loop, the function of the continue statement is to skip the code that is not executed in the current loop and directly execute the next loop.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.