Results 1 to 4 of 4

Thread: Program to break out of multiple loops

  1. #1
    Join Date
    Jan 2009
    Posts
    48

    Program to break out of multiple loops

    Hi all,

    I am trying to write a program, which will require breaking out of multiple loops.
    I am totally confused regarding this. I have tried many methods to achieve this but not successful.

    I have to break out of two loops, but thing is that another loop also has the program code.
    Is there any way to achieve this? Please help me if you can...

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: Program to break out of multiple loops

    Please see the below code, which shows how to break out of the lowest loop from the multiple loops:

    while(condition 1)
    {
    do_it1();
    while(condition 2)
    {
    do_it2();
    while (condition 3)
    {
    doit3();
    if (condition 4)
    {
    // break out to condition 1 while loop
    }
    }
    do_it4();
    }
    do_it5();
    }
    Try above logic in your program..

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Program to break out of multiple loops

    Hi friend,

    You can achieve "Breaking out of multiple loops" using "labels" in the program code. I have tried "labels"and program executed successful. Below is the program which will show you the use of "label":

    class Break_Label_Demo {
    public static void main(String[] args) {

    int[][] arrayOfInts = { { 35, 77, 3, 589 },
    { 32, 1086, 1000, 7 },
    { 682, 147, 97, 975 }
    };
    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 list");
    }
    }
    }

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Program to break out of multiple loops

    Hi,

    Solution for your problem is very easy. I am not giving you entire answer ,But I will give you hint through below program to "break out of multiple loops":

    package Random;

    public class Break_While_Loop {


    public static void main(String[] args) {

    int i=0;
    boolean loop_One_Break = false;
    while (true){ //while 1
    while(true){ //while 2
    while(true){ //while 3
    i++;
    if(i>5){
    loop_One_Break = true;
    }
    if (loop_One_Break){
    break; //break while 3
    }
    System.out.println("while 3: " + i);
    }
    if (loop_One_Break){
    break; // break while 2
    }
    System.out.println("while 2: " + i);
    }
    if (loop_One_Break){
    break; //break while 1
    }
    System.out.println("while 1: " + i);
    }
    System.out.println("Ended While loops succsessfully!");

    }

    }

Similar Threads

  1. Replies: 2
    Last Post: 17-02-2012, 02:09 PM
  2. Replies: 5
    Last Post: 14-12-2011, 06:09 PM
  3. Can we have multiple try and Catch block in our program ?
    By Solaris in forum Software Development
    Replies: 4
    Last Post: 29-01-2011, 11:52 PM
  4. Line break in PHP program
    By Aidan 12 in forum Software Development
    Replies: 4
    Last Post: 04-03-2010, 10:49 PM
  5. How to use the break statement in java program?
    By Madaleno in forum Software Development
    Replies: 4
    Last Post: 23-01-2010, 09:27 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,251,376.58119 seconds with 17 queries