Introduction to the author
Author name: Ming Shiyin in programming world
Introduction: CSDN blog expert has been engaged in software development for many years and is proficient in Java and JavaScript. Bloggers also learn and grow step by step from scratch, know the importance of learning and accumulation, and like to fight and upgrade with ADC. Welcome to pay attention and look forward to learning, growing and taking off with you!
introduction
Many Java beginners ask me that it's worrying for the novice to turn around and forget the Java knowledge he has studied very hard. How can Xiaobai grow up quickly and become a big cow?
In fact, there is only one skill to become a big God: "learn more and practice more", so brother Ming sorted out typical practice examples. Through practice, you can quickly improve coding skills and proficiency, so that you can never return on the way to become a big man (remember to practice with your own hands)!
Navigation
✪ introduction to Java white 200 case series directory index
◄ previous article 47. Detailed explanation of break statement
► next 49. return statement in the loop
describe
Sometimes we don't want to repeat some logic, but we don't want to exit the loop directly, so java provides a continue statement to skip the current loop.
The continue statement is to skip the remaining statements in the loop body and force the execution of the next loop. Its function is to end the loop, that is, skip the following unexecuted statements in the loop body, and then determine whether to execute the loop next time.
Example 1
Normal for loop without continue statement
package demo.demo48; public class Test2 { public static void main(String[] args) { //Cyclic printing for (int i = 0; i < 5; i++) { System.out.println("Print:"+i); } } }
Operation results:
Print: 0
Print: 1
Print: 2
Print: 3
Print: 4
Example 2
If I don't want to print 2, I can add the continue statement as follows:
package demo.demo48; public class Test2 { public static void main(String[] args) { //Cyclic printing for (int i = 0; i < 5; i++) { if(i==2){//When i=2, skip this cycle and determine the next cycle continue; } System.out.println("Print:"+i); } } }
Operation results:
Print: 0
Print: 1
Print: 3
Print: 4
You can see that 2 is ignored, that is, when 2 is encountered in the code, the following code is ignored and judge the next cycle, which is very different from break. Break directly interrupts the cycle.
Example 3
Use continue in nested loops
package demo.demo48; public class Test3 { public static void main(String[] args) { // External circulation for (int i = 0; i < 5; i++) { System.out.print("The first" + (i + 1) + "Second cycle:"); // Internal circulation, designed as 6 cycles for (int j = 0; j < 6; j++) { // Judge whether j is equal to 3. If so, terminate the cycle if (j == 3) { continue ; } System.out.print("Second stage of internal circulation" + (j + 1) + "second\t"); } System.out.println(); } } }
Operation results:
1st cycle: 1st internal cycle, 2nd internal cycle, 3rd internal cycle, 5th internal cycle, 6th internal cycle
2nd cycle: 1st internal cycle, 2nd internal cycle, 3rd internal cycle, 5th internal cycle, 6th internal cycle
3rd cycle: 1st internal cycle, 2nd internal cycle, 3rd internal cycle, 5th internal cycle, 6th internal cycle
4th cycle: 1st internal cycle, 2nd internal cycle, 3rd internal cycle, 5th internal cycle, 6th internal cycle
5th cycle: 1st internal cycle, 2nd internal cycle, 3rd internal cycle, 5th internal cycle, 6th internal cycle
You can see that continue skipped the fourth cycle.
Example 4
Tagged continue statement
package demo.demo48; public class Test4 { public static void main(String[] args) { // External circulation out:for (int i = 0; i < 5; i++) { System.out.print("The first" + (i + 1) + "Second cycle:"); // Internal circulation, designed as 6 cycles for (int j = 0; j < 6; j++) { // Judge whether j is equal to 3. If so, terminate the cycle if (j == 3) { System.out.println(); continue out; } System.out.print("Second stage of internal circulation" + (j + 1) + "second\t"); } System.out.println(); } } }
Operation results:
1st cycle: 1st cycle of internal cycle, 2nd cycle of internal cycle, 3rd cycle of internal cycle
Second cycle: the first time of internal cycle, the second time of internal cycle and the third time of internal cycle
3rd cycle: 1st cycle of internal cycle 2nd cycle of internal cycle 3rd cycle of internal cycle
The 4th cycle: the 1st internal cycle, the 2nd internal cycle and the 3rd internal cycle
The 5th cycle: the 1st internal cycle, the 2nd internal cycle and the 3rd internal cycle
You can see that this continue will directly skip all the remaining statements in the inner loop because
When j ==3, continue out skips the outer loop. Originally, the outer loop was to be executed 6 times. Now when j ==3, the current loop of the outer loop is directly skipped, so it is only executed to 3.
Of course, some people may ask, is it possible to use continue in the while loop? Let's try it.
Example 5
package demo.demo48; public class Test { public static void main(String[] args) { int num=0; while (num<10) { //Print System.out.println("Print numbers:"+num); //Increasing num++; if(num==5){ continue; } } } }
Operation results:
Print number: 0
Print number: 1
Print number: 2
Print number: 3
Print number: 4
Print number: 5
Print number: 6
Print number: 7
Print number: 8
Print number: 9
The original intention is to skip printing when i==5, but the test results show that continue in the while loop does not take effect.
Summary
This section summarizes the "detailed explanation of continue statement". I hope it can be helpful to you. Please help [like] + [collection] + [punch in the comment area]. If you are interested in learning Java with brother Xiaoming, [pay attention to a wave] won't get lost.
Let me know you by punching in the comment area. Mingge will continue to pay attention to your learning progress!
Navigation
✪ introduction to Java white 200 case series directory index
◄ previous article 47. Detailed explanation of break statement
► next 49. return statement in the loop
Popular column recommendation
1.Java game series (Tetris, aircraft war, plant war, zombie, etc.)
2.JavaWeb project practice (library management, online examination, dormitory management system, etc.)
3. Wonderful examples of JavaScript (aircraft war, minesweeping, snake eating, verification code, etc.)
4. Introduction to Java Xiaobai 200 cases
5. Learn Java from zero, learn Java with interest, and learn Java from the perspective of King glory