said in the previous sentence:
Today, when writing a program, I need to jump out of the loop. I can’t remember, but I checked online, as follows:
as you know, in Java, if you want to break out of a for loop, there are generally two ways: break and continue.
break is a break out of the current for loop, as shown in the following code:
package com.xtfggef.algo;
public class RecTest {
/**
* @param args
*/
public static void main(String[] args) {
for(int i=0; i<10; i++){
if(i==5){
break;
}
System.out.print(i+" ");
}
}
}
output: 0 1 2 3 4
means that the break breaks out of the current loop.
continue is to break out of the current loop and open the next loop, as shown below:
package com.xtfggef.algo;
public class RecTest {
/**
* @param args
*/
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
System.out.print(i+" ");
}
}
}
output: 0 1 2 3 4 6 7 8 9
jump out of a multi-layer loop
the above two methods cannot jump out of a multi-layer loop. If you need to jump out of a multi-layer loop, you need to use a label, define a label, and then you need to jump
, use the break label line, the code is as follows:
package com.xtfggef.algo;
public class RecTest {
/**
* @param args
*/
public static void main(String[] args) {
loop: for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
for (int k = 0; k < 10; k++) {
for (int h = 0; h < 10; h++) {
if (h == 6) {
break loop;
}
System.out.print(h);
}
}
}
}
System.out.println("\nI'm here!");
}
}
Output:
012345
I’m here!
the meaning is obvious!
Read More:
- Idea start error: java.lang.IllegalStateException : failed to create a child event loop
- Java compareto() method
- On the intern () method of string class in Java
- Java and Android commons codec usage and nosuch method error
- How to generate main () method in java graphical interface
- [resolved] exception java.net.ConnectException : Error opening socket to server Connection timed out.
- java.sql.SQLException : IO error: socket read timed out!
- java.sql.SQLException : IO exception: socket read timed out
- Error: JMeter monitors Linux system performance java.net.ConnectException : Connection timed out: connect
- java.lang.IllegalStateException: Could not execute method for android:onClick
- Difference between isempty method and isblank method in stringutils
- Caused by: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0
- Error java.lang.IllegalArgumentException: Validation failed for query for method public abstract
- Java uses the createnewfile() method to report an error
- Spock + powermock simulation static method reports an error java.lang.classcastexception
- Linux shell loop in a line for while
- Caused by: java.lang.IllegalStateException: Ambiguou There is already ‘XXXXXXController‘ bean method
- ‘break’ not in the ‘loop’ or ‘switch’ context Error (Fixed)
- error: ‘for‘ loop initial declarations are only allowed in C99 mode
- Method threw ‘java.lang.NullPointerException‘ exception. Cannot evaluate com.sun.proxy.xxx