Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Breaking out of multiple loops

  1. #1
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Breaking out of multiple loops

    Is there an easy way to do it? I want to break out of two loops, but the code is also in another loop.


  2. #2
    Little Star
    Join Date
    May 2009
    Posts
    30
    Thanks
    0
    Thanked 9 Times in 7 Posts

    Default Re: Breaking out of multiple loops

    Hi,
    have you tried using:
    break;
    ?

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Breaking out of multiple loops

    break only breaks out of the lowest loop you're in.
    ex:
    while(cond1)
    {
            doit1();
            while(cond2)
            {
                        doit2();
                         while (cond3)
                        {
                                doit3();
                                if (cond4)
                                {
                                       // break out to cond1 while loop
                                }
                        }
                        doit4();
            }
            doit5();
    }

    what would you put in place of the comment to fulfill that task?

  4. #4
    Little Star
    Join Date
    May 2009
    Posts
    30
    Thanks
    0
    Thanked 9 Times in 7 Posts

    Default Re: Breaking out of multiple loops

    I wrote a short code to test my theory and it worked, however I believe you can reuse and optimize the code in a better way to get the same results, this is just an example how to do it:

    package Random;
     
    public class BreakWhileLoops {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
     
    		int i=0;
    		boolean loopOneBreak = false;
    		while (true){ //while 1
    			while(true){ //while 2
    				while(true){ //while 3
    					i++;
    					if(i>5){
    						loopOneBreak = true;
    					}
    					if (loopOneBreak){
    						break; //break while 3
    					}
    					System.out.println("while 3: " + i);
    				}
    				if (loopOneBreak){
    					break; // break while 2
    				}
    				System.out.println("while 2: " + i);
    			}
    			if (loopOneBreak){
    				break; //break while 1
    			}
    			System.out.println("while 1: " + i);
    		}
    		System.out.println("Ended while loops succsessfully!");
     
    	}
     
    }

  5. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Breaking out of multiple loops

    Using labels allows you to break out of several loops at once.

    class BreakWithLabelDemo {
        public static void main(String[] args) {
     
            int[][] arrayOfInts = { { 32, 87, 3, 589 },
                                    { 12, 1076, 2000, 8 },
                                    { 622, 127, 77, 955 }
                                  };
            int searchfor = 12;
     
            int i;
            int j = 0;
            boolean foundIt = false;
     
        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;
                    }
                }
            }
     
            if (foundIt) {
                System.out.println("Found " + searchfor +
                                   " at " + i + ", " + j);
            } else {
                System.out.println(searchfor
                                   + " not in the array");
            }
        }
    }

    // Json

  6. The Following User Says Thank You to Json For This Useful Post:

    helloworld922 (July 2nd, 2009)

Similar Threads

  1. traversing multiple jTabbedPane?
    By dewboy3d in forum AWT / Java Swing
    Replies: 3
    Last Post: October 2nd, 2009, 08:26 PM
  2. Which interface gives more control on serialization of an object?
    By abhishekraok2003 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 16th, 2009, 11:17 AM
  3. [SOLVED] Need to display multiple images from database on a webpage
    By raghuprasad in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: May 13th, 2009, 04:15 AM
  4. Replies: 11
    Last Post: April 29th, 2009, 04:12 AM
  5. [SOLVED] Fixing of bug in number guessing game
    By big_c in forum Loops & Control Statements
    Replies: 6
    Last Post: April 16th, 2009, 03:15 AM