user1166085
user1166085

Reputation: 627

How to break out of a while loop in java?

// Setup dummy array
    ArrayList<Integer> list = dateArray;
    int counter = 1;
    while (list.size() != 0) {
        for (int j = 1; j < list.size(); j++)
        {

            //System.out.println(list.get(0) + " and " + list.get(j));
            int difference = list.get(0) - list.get(j);
            if (difference <6){
                System.out.println(list.get(0) + " and " + list.get(j) + " and size is " +list.size() );    
                counter= counter +1;
                System.out.println ("Counter is " + counter);
                if (counter >= 4){
                    System.out.println ("j = " + j + " Counter =" + counter);   
                    if (j ==list.size()-1) {
                        System.out.println ("here " + counter); 
                        break;
                    }   
                }
            }
        }
        list.remove(0);
    };  

Output:

1 and 2 and size is 4  
Counter is 2  
1 and 3 and size is 4  
Counter is 3  
1 and 4 and size is 4  
Counter is 4  
here 4  
2 and 3 and size is 3  
Counter is 5  
3 and 4 and size is 2  
Counter is 6  
here 6  

Ideally, i want it to stop when Counter is 4 and don't go on to execute "2 and 3 and size is 3 "

Much appreciated!

Upvotes: 0

Views: 459

Answers (4)

Artiel
Artiel

Reputation: 136

You can add a boolean variable like this:

// Setup dummy array
ArrayList<Integer> list = dateArray;
int counter = 1;
boolean isBreak = false;
while (list.size() != 0 && !isBreak) {
    for (int j = 1; j < list.size(); j++)
    {

        //System.out.println(list.get(0) + " and " + list.get(j));
        int difference = list.get(0) - list.get(j);
        if (difference <6){
            System.out.println(list.get(0) + " and " + list.get(j) + " and size is " +list.size() );    
            counter= counter +1;
            System.out.println ("Counter is " + counter);
            if (counter >= 4){
                System.out.println ("j = " + j + " Counter =" + counter);   
                if (j ==list.size()-1) {
                    System.out.println ("here " + counter); 

                    isBreak = true;
                    break;
                }   
            }
        }
    }
    list.remove(0);
}; 

Upvotes: 0

Ashish Sharma
Ashish Sharma

Reputation: 41

You can use java label for that as:

    int x = 1;
    outerLoopLabel:
    while(x != 10) {
      int fact = 0;      
      for(int i=0;i <=x; i++) {        
        fact +=i;
        if(fact > 25) {
          break outerLoopLabel;    
        }
      }
      System.out.println("Number: "+ x + " and fact: " + fact);      
      ++x;      
    }

Output without label :

Number: 1 and fact: 1
Number: 2 and fact: 3
Number: 3 and fact: 6
Number: 4 and fact: 10
Number: 5 and fact: 15
Number: 6 and fact: 21
Number: 7 and fact: 28
Number: 8 and fact: 36
Number: 9 and fact: 45

Output with label:

Number: 1 and fact: 1
Number: 2 and fact: 3
Number: 3 and fact: 6
Number: 4 and fact: 10
Number: 5 and fact: 15
Number: 6 and fact: 21

Upvotes: 1

sauumum
sauumum

Reputation: 1788

You need to use a label for the loop . Please try below snippet :

// Setup dummy array
    ArrayList<Integer> list = dateArray;
    int counter = 1;
outerwhileloop:
    while (list.size() != 0) {
        for (int j = 1; j < list.size(); j++)
        {

            //System.out.println(list.get(0) + " and " + list.get(j));
            int difference = list.get(0) - list.get(j);
            if (difference <6){
                System.out.println(list.get(0) + " and " + list.get(j) + " and size is " +list.size() );    
                counter= counter +1;
                System.out.println ("Counter is " + counter);
                if (counter >= 4){
                    System.out.println ("j = " + j + " Counter =" + counter);   
                    if (j ==list.size()-1) {
                        System.out.println ("here " + counter); 
                        break outerwhileloop;
                    }   
                }
            }
        }
        list.remove(0);
    };      

Upvotes: 2

Ali
Ali

Reputation: 12674

Just move the code to a method and call return. Or else, you can use something called labeled break.

search:
    for (i = 0; i < arrayOfInts.length; i++) {
        for (j = 0; j < arrayOfInts[i].length;
             j++) {
            if (arrayOfInts[i][j] == searchfor) {
                foundIt = true;
                break search;
            }
        }
    }

Upvotes: 1

Related Questions