Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 24-11-2009
Member
 
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...
Reply With Quote
  #2  
Old 24-11-2009
Member
 
Join Date: May 2008
Posts: 2,383
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:

Quote:
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..
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #3  
Old 24-11-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
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":

Quote:
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");
}
}
}
Reply With Quote
  #4  
Old 24-11-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
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":

Quote:
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!");

}

}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Program to break out of multiple loops"
Thread Thread Starter Forum Replies Last Post
Page Break lines are blocked under Page Break Preview in Microsoft Excel connoisseur MS Office Support 2 17-02-2012 02:09 PM
Difference between Titan Break 1 and Titan Break 2 in Infinity Blade 2 TeesMaar Video Games 5 14-12-2011 06:09 PM
Can we have multiple try and Catch block in our program ? Solaris Software Development 4 29-01-2011 11:52 PM
Line break in PHP program Aidan 12 Software Development 4 04-03-2010 10:49 PM
How to use the break statement in java program? Madaleno Software Development 4 23-01-2010 09:27 PM


All times are GMT +5.5. The time now is 09:44 PM.