This article is a mirror article of machine translation, please click here to jump to the original article.

View: 18829|Reply: 0

[Communication] How many loops can a break jump out? How can I jump out of all loops?

[Copy link]
Posted on 12/9/2015 11:12:49 AM | | |

How many loops can break out of a multi-layer loop at a time?
Practice is the only criterion for verifying the truth, and multi-hands type code for experiments:

  1. public class BreakTest
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 for(int x=0; x<2; x++)
  6.                 {
  7.                         for(int y=0; y<2; y++)
  8.                         {
  9.                                 for(int z=0; z<2; z++)
  10.                                 {
  11.                                         if(z==0) break;
  12.                                         System.out.println("----z="+z);
  13.                                 }
  14.                                 System.out.println("--y="+y);
  15.                         }
  16.                         System.out.println("x="+x);
  17.                 }
  18.         }
  19. }
Copy code
Output:

  1. --y=0
  2. --y=1
  3. x=0
  4. --y=0
  5. --y=1
  6. x=1
Copy code


This shows that break can only jump out of one loop, (if it is in the inner loop, the inner loop ends, if it is in the outer loop, both inside and outside end.)
break is to end the current loop,
continue is to end this cycle and proceed to the next cycle,
Using break is no longer looping
Use continue the loop

If you want to jump out of all loops, just use return!




Previous:C language, full version video
Next:WPF and Silverlight projects use bar charts, pie charts, and line charts
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com