how to break two loop in java
Java
import java.io.IOException;
/**
* How to break from nested loop in Java. You can use labeled
* statement with break statement to break from nested loop.
*
* @author WINDOWS 8
*/
public class BreakingFromNestedLoop{
public static void main(String args[]) throws IOException {
// this is our outer loop
outer: for (int i = 0; i < 4; i++) {
// this is the inner loop
for (int j = 0; j < 4; j++) {
// condition to break from nested loop
if (i * j > 5) {
System.out.println("Breaking from nested loop");
break outer;
}
System.out.println(i + " " + j);
}
}
System.out.println("exited");
// better way is to encapsulate nested loop in a method
// and use return to break from outer loop
breakFromNestedLoop();
}
/**
* You can use return statement to return at any point from a method.
* This will help you to break from nested loop as well
*/
public static void breakFromNestedLoop(){
for(int i=0; i<6; i++){
for(int j=0; j<3; j++){
int product = i*j;
if(product > 4){
System.out.println("breaking from nested loop using return");
return;
}
}
}
System.out.println("Done");
}
}
Output
0 0
0 1
0 2
0 3
1 0
1 1
1 2
1 3
2 0
2 1
2 2
Breaking from nested loop
exited
breaking from nested loop using return
Also in Java:
- Title
- java create new object
- Category
- Java
- Title
- java stream sort Collator
- Category
- Java
- Title
- recursion in java
- Category
- Java
- Title
- spring code in java
- Category
- Java
- Title
- how to replace in java
- Category
- Java
- Title
- random code
- Category
- Java
- Title
- add video in bootstrap
- Category
- Java
- Title
- java 8 list stream delete by name
- Category
- Java
- Title
- string method example in java
- Category
- Java
- Title
- stream java example
- Category
- Java
- Title
- how to add an object to a list of objects in java
- Category
- Java
- Title
- java setroundingmode
- Category
- Java
- Title
- pretty print json in console
- Category
- Java
- Title
- how do i print text in java
- Category
- Java
- Title
- java how to call getReader twice
- Category
- Java
- Title
- cosinus-1 java
- Category
- Java
- Title
- hashmap get value java
- Category
- Java
- Title
- java string contains char
- Category
- Java
- Title
- get file path java
- Category
- Java
- Title
- bukkit java get max players
- Category
- Java
- Title
- osmdroid offline map does not show
- Category
- Java
- Title
- sort a map based on keys and values using java 8
- Category
- Java
- Title
- if else program in java
- Category
- Java
- Title
- Filebody in java
- Category
- Java
- Title
- waht does&& mean in java
- Category
- Java
- Title
- get raondom from array java
- Category
- Java
- Title
- system.out.println shortcut
- Category
- Java
- Title
- how to add all list elements at once in java
- Category
- Java
- Title
- binary number input in int java
- Category
- Java
- Title
- java test if a string is a int
- Category
- Java
- Title
- java map string to list
- Category
- Java
- Title
- java method overriding
- Category
- Java
- Title
- iterate trough linked list java
- Category
- Java
- Title
- java random boolean
- Category
- Java
- Title
- java string array to arraylist
- Category
- Java
- Title
- using java 8 stream to process data in java
- Category
- Java
- Title
- how to check if the file has remaining without reading from it java
- Category
- Java
- Title
- ARE THERE POINTER IN JAVA
- Category
- Java
- Title
- Scanner library showing element not found exception
- Category
- Java
- Title
- java stack push
- Category
- Java
- Title
- how take inputs in java with message
- Category
- Java
- Title
- array of objects in java
- Category
- Java
- Title
- java hashmap syntax
- Category
- Java
- Title
- java size of stack
- Category
- Java
- Title
- eclipse does not support logback with spring boot
- Category
- Java
- Title
- create java windows application
- Category
- Java
- Title
- java ternary operator
- Category
- Java
- Title
- convert string to array java
- Category
- Java
- Title
- how to crate a list in java script
- Category
- Java
- Title
- how to pass a float between activities in android studio
- Category
- Java
- Title
- get day name from date in java
- Category
- Java
- Title
- java loop through arraylist
- Category
- Java
- Title
- java http interceptor
- Category
- Java
- Title
- java remove non numbers from string
- Category
- Java
- Title
- import scanner
- Category
- Java
- Title
- java nested loop
- Category
- Java
- Title
- java fx display simple rectangle
- Category
- Java
- Title
- java how to change the length of an array
- Category
- Java
- Title
- toString convert to long
- Category
- Java
- Title
- for next loop javasxcrop
- Category
- Java
- Title
- official java website
- Category
- Java
- Title
- print a list java
- Category
- Java
- Title
- java script using navigator.useragent to detect the browse
- Category
- Java
- Title
- how to init an array with objects in java
- Category
- Java
- Title
- @fortawesome/free-solid-svg-icons
- Category
- Java
- Title
- write in file java
- Category
- Java
- Title
- tolowercase java
- Category
- Java
- Title
- java random number between 100 and 999
- Category
- Java
- Title
- how to print a 2d array in java
- Category
- Java
- Title
- create jdbc connection in java
- Category
- Java
- Title
- trim() and split() in java
- Category
- Java
- Title
- log10 in java
- Category
- Java
- Title
- creating the functional interface in java
- Category
- Java
- Title
- convert list to array in java
- Category
- Java
- Title
- how to find powers in java
- Category
- Java
- Title
- how to use spring in java
- Category
- Java
- Title
- java arraylist to string
- Category
- Java
- Title
- java projects
- Category
- Java
- Title
- what is a float java
- Category
- Java
- Title
- socket in java
- Category
- Java
- Title
- arraylist of double
- Category
- Java
- Title
- java swing timer sleep
- Category
- Java
- Title
- convert array of int to arraylist java
- Category
- Java
- Title
- java null pointer exception
- Category
- Java
- Title
- arraylist with values
- Category
- Java
- Title
- convert date to calendar java
- Category
- Java
- Title
- java unit test an api
- Category
- Java
- Title
- java mysql springboot jpa stackoverflow
- Category
- Java
- Title
- if statement java
- Category
- Java
- Title
- android studio java random number generator
- Category
- Java
- Title
- change button color java swing
- Category
- Java
- Title
- sort elements with sortedset
- Category
- Java
- Title
- double round java integer
- Category
- Java
- Title
- how to make a list java
- Category
- Java
- Title
- how add strings together
- Category
- Java
- Title
- how to make a button disapear on click in javafx
- Category
- Java
- Title
- how to do a linear searc in java
- Category
- Java
- Title
- creating a program about user asked to enter a number and enters 0 to stop, then calculates the sum of numbers entered, the average, the length of positive numbers and negative numbers entered by the user in java
- Category
- Java
- Title
- how to change tablayout current view position in android
- Category
- Java
- Title
- java catch multiple exceptions
- Category
- Java