Like other answerers, I’d definitely prefer to put the loops in a different method, at which point you can just return to stop iterating completely. This answer just shows how the requirements in the question can be met.
You can use break
with a label for the outer loop. For example:
public class Test { public static void main(String[] args) { outerloop: for (int i=0; i < 5; i++) { for (int j=0; j < 5; j++) { if (i * j > 6) { System.out.println("Breaking"); break outerloop; } System.out.println(i + " " + j); } } System.out.println("Done"); } }
This prints:
0 0 0 1 0 2 0 3 0 4 1 0 1 1 1 2 1 3 1 4 2 0 2 1 2 2 2 3 Breaking Done
Related Posts:
- Iterate through a HashMap [duplicate]
- Iterate through a HashMap [duplicate]
- How do I apply the for-each loop to every character in a String?
- Java compressing Strings
- What condition does while(true) test? When is it true and false?
- Bank Account Java Program
- How would I use a while loop to keep requesting user input
- Create a two dimensional string array anArray[2][2]
- How to use a Do-while loop that continuously prompts a user?
- How to make a Java Main Menu Loop after using a case
- Is there an invisible character that is not regarded as whitespace?
- 1000 * 60 * 60 * 24 * 30 results in a negative number [duplicate]
- What is the difference between x86 and x64
- What does “Could not find or load main class” mean?
- Implementing UML diagram to Java [closed]
- Reading a plain text file in Java
- Converting double to string
- Java Error “Exception in thread “main” java.util.InputMismatchException” On an Array program
- Basic calculator in Java
- Converting double to integer in Java
- Difference between HashSet and HashMap?
- How to split a String by space
- Java compile error: “reached end of file while parsing }”
- How can I convert a long to int in Java?
- Generic Interface
- Getting a HeadlessException: No X11 DISPLAY variable was set
- What is the exact meaning of instantiate in JAVA
- Exception in thread “main” java.util.NoSuchElementException: No line found
- Java Method Call Expected
- Double % formatting question for printf in Java
- Why is there no Constant feature in Java?
- Error creating bean with name ‘entityManagerFactory’ defined in class path resource : Invocation of init method failed
- Printing out a linked list using toString
- Using @RequestLine with Feign
- What is the best/simplest way to read in an XML file in Java application?
- Received fatal alert: handshake_failure through SSLHandshakeException
- DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704
- Why would a “java.net.ConnectException: Connection timed out” exception occur when URL is up?
- HashSet vs. ArrayList
- FIFO based Queue implementations?
- How to import a .cer certificate into a java keystore?
- Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0 error in Java
- How can I represent a range in Java?
- What is a safe way to create a Temp file in Java?
- Android java.lang.IllegalStateException: Could not execute method of the activity
- Java error: Comparison method violates its general contract
- “Cannot create generic array of ..” – how to create an Array of Map
? - javac: file not found: first.java Usage: javac
- Eclipse Java error: This selection cannot be launched and there are no recent launches
- java.lang.ArithmeticException: / by zero
- Eclipse error, “The selection cannot be launched, and there are no recent launches”
- STS launch error – Java was started but returned exit code=13
- RecyclerView onClick
- Java, Shifting Elements in an Array
- error: bad operand types for binary operator ‘&&’
- Exception in thread “main” java.lang.ArithmeticException: / by zero
- Failed to load the JNI shared Library (JDK)
- “com.jcraft.jsch.JSchException: Auth fail” with working passwords
- java.util.NoSuchElementException – Scanner reading user input
- Array Required, but java.lang.String found
- Eclipse java debugging: source not found
- IntelliJ – Can’t start – “JAVA_HOME does not point to a valid JVM installation”
- Empty an array in Java / processing
- Class is not Abstract and does not Override error in Java
- Adding days to a date in Java
- The C# Shorthand getters and setters
- What’s the reason I can’t create generic array types in Java?
- How to pass an object from one activity to another on Android
- Finding the size of a char array in Java
- Android For loop
- Java project in Eclipse: The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files
- Replace a character at a specific index in a string?
- Error array dimension missing
- How to detect EOF in Java?
- Information about _JAVA_OPTIONS
- Two dimensional array list
- How do you set one array’s values to another array’s values in Java?
- ‘No JUnit tests found’ in Eclipse
- Popup Message boxes
- How to create a Multidimensional ArrayList in Java?
- How do I get the last character of a string?
- Java equivalent to C# extension methods
- Java: is there an easy way to select a subset of an array?
- void is an invalid type for this variable
- syntax error: insert } to complete ClassBody
- How to print the value of variable in java [duplicate]
- File to byte[] in Java
- Eclipse: JVM terminated. Exit code=2
- How to programmatically close a JFrame
- Playing .mp3 and .wav in Java?
- Wrong project is being run in Eclipse
- Multidimensional Arrays lengths in Java
- atoi in java using charAt
- Quotation marks inside a string
- XY Plotting with Java
- Mockito : doAnswer Vs thenReturn
- How Do I Implement an Insertion Sort Method for A Generic ArrayList?
- Troubleshooting “The type parameter T is hiding the type T” warning
- model.addAttribute() parameters
- StringFormat for Java Boolean Operator