Results 1 to 6 of 6
  1. #1
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default breaking out of while loop

    suppose your code looks like this:

    Java Code:
    while ((currentInputLine = br.readLine()) != null) {
     for (i=0; i< 35; i++) {
          if (i == 13) {
            // at this point i need to breakout of for loop AND move onto the next line in while loop.
    
         }
      }
    }

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    If there is no code after the for loop, breaking out of the for loop would mean you proceed to the next while loop iteration. If there is code after the for loop, I'd suggest moving the for loop into a separate method, that returns a boolean, then you can have a continue statement inside the while loop.
    Java Code:
    void method1() {
      while(some condition) {
        if(method2()) continue;
        rest of code
      }
    }
    
    boolean method2() {
      for(int i = 0; i < blabla; i++) {
        if(some condition) return true;
        rest of code
      }
      return false;
    }
    If the for loop in method 2 runs it's course, the rest of code in the while loop will execute, but if the break condition inside the for loop in method 2 occurs, the rest of the code in the while loop will not execute.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    xerberuz is offline Member
    Join Date
    Apr 2010
    Posts
    8
    Rep Power
    0

    Default

    Java Code:
    outer_loop: while (true)
          {
             for (int i = 0; i < 5; i++)
             {
                if (i == 4)
                {
                   continue outer_loop;
                }
                else
                {
                   System.out.println(i);
                }
             }
          }
    It's similar to goto in other languages. In java it will only work for nested loops.

    IMHO this is a ugly solution, but it works. So this should be used rarely.

    If there is no other code in the while loop a simple break will do the trick.
    Last edited by xerberuz; 05-18-2010 at 04:01 PM.

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    You could always just use a break...
    Java Code:
    while ((currentInputLine = br.readLine()) != null) {
         for (i=0; i< 35; i++) {
              if (i == 13) {
                   break;// at this point i need to breakout of for loop AND move onto the next line in while loop.
              }
         }
    }

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Goto solutions should be avoided, and to StormyWaters, I think the OP has code after the inner for loop as well, but I did cover that eventuality in my post too.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  6. #6
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    Oops, sorry didn't see the AND in there, guess I was oversimplifying the problem

Similar Threads

  1. breaking up a string, a regex problem!!
    By A.n.H in forum Advanced Java
    Replies: 0
    Last Post: 05-17-2010, 04:03 PM
  2. Breaking up of array
    By agarwal_srushti in forum New To Java
    Replies: 3
    Last Post: 09-27-2009, 08:03 PM
  3. Breaking for-loops with listeners?
    By CBarry in forum New To Java
    Replies: 3
    Last Post: 04-22-2009, 04:38 AM
  4. Breaking down an integer
    By Emily in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 07:39 PM
  5. Breaking from nested switch
    By javaplus in forum New To Java
    Replies: 3
    Last Post: 02-02-2008, 09:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •