break through 2 loops
-
Hey everyone, I would like to break through 2 loops with just one rule. Something like this:
for(;;)//loop 1
{
for(;;)//loop 2
{
//do something to exit loop 1
}
}Hi, there are several ways of breaking out of several loops: 1. change the state such that all the continuation tests will fail, and break (one way of achieving this is by adding a boolean test to each and every continuation test) 2. create a method that contains nothing but the loops, and return where you want to break out; 3. throw an exception and catch it where you need to. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
-
Hey everyone, I would like to break through 2 loops with just one rule. Something like this:
for(;;)//loop 1
{
for(;;)//loop 2
{
//do something to exit loop 1
}
} -
Hi, there are several ways of breaking out of several loops: 1. change the state such that all the continuation tests will fail, and break (one way of achieving this is by adding a boolean test to each and every continuation test) 2. create a method that contains nothing but the loops, and return where you want to break out; 3. throw an exception and catch it where you need to. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
-
Yes I've thought of those options too, but I don't want to break with extra code, not even a boolean. With 'break;' you will exit one loop. But isn't there something like break * 2;
Deresen wrote:
But isn't there something like break * 2;
You will end up in
goto
.Navaneeth How to use google | Ask smart questions
-
Yes I've thought of those options too, but I don't want to break with extra code, not even a boolean. With 'break;' you will exit one loop. But isn't there something like break * 2;
Hi, No. Although there are languages that support
break <count>
C# isn't one of them. :)Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
-
Hi, there are several ways of breaking out of several loops: 1. change the state such that all the continuation tests will fail, and break (one way of achieving this is by adding a boolean test to each and every continuation test) 2. create a method that contains nothing but the loops, and return where you want to break out; 3. throw an exception and catch it where you need to. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
Luc Pattyn wrote:
3. throw an exception and catch it where you need to.
Exceptions to control program flow? Isn't that a crime?
Regards, Rob Philpott.
-
Luc Pattyn wrote:
3. throw an exception and catch it where you need to.
Exceptions to control program flow? Isn't that a crime?
Regards, Rob Philpott.
Hi,
Rob Philpott wrote:
Exceptions to control program flow? Isn't that a crime?
Not always. The OP didn't mention why nor how often he would break out of all loops. Exceptions always operate on program flow, they exist to make life easier and code more reliable. Not making good use of available means would be a crime. Depending on circumstances, one has to make a good choice, that is why I gave three possibilities. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
-
Hi, there are several ways of breaking out of several loops: 1. change the state such that all the continuation tests will fail, and break (one way of achieving this is by adding a boolean test to each and every continuation test) 2. create a method that contains nothing but the loops, and return where you want to break out; 3. throw an exception and catch it where you need to. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
4.use
goto
. :rolleyes: :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Luc Pattyn wrote:
3. throw an exception and catch it where you need to.
Exceptions to control program flow? Isn't that a crime?
Regards, Rob Philpott.
Rob Philpott wrote:
Exceptions to control program flow? Isn't that a crime?
Nope, if the condition that breaks the loops is exceptional. :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
4.use
goto
. :rolleyes: :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Absolutely. That is covered by "Not making good use of available means would be a crime". :-D
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
-
Rob Philpott wrote:
Exceptions to control program flow? Isn't that a crime?
Nope, if the condition that breaks the loops is exceptional. :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Agreed. But use otherwise deserves a slap!
Regards, Rob Philpott.
-
Absolutely. That is covered by "Not making good use of available means would be a crime". :-D
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
Luc Pattyn wrote:
Absolutely
I agree, in fact this is the perfect example of when a goto may be the most appropriate. It can be a useful statement - it's only because it can be (and has been) wildly misused that it's frowned upon. :-D
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
Hey everyone, I would like to break through 2 loops with just one rule. Something like this:
for(;;)//loop 1
{
for(;;)//loop 2
{
//do something to exit loop 1
}
}If you were using Java, I'd say "break OuterLoop;" (by adding the label "OuterLoop:" before the outer loop). But since you're in C#, you need to do this:
for ( ; ; )
{
for ( ; ; )
{
goto EndOfLoop;
}
}
EndOfLoop:
// your code(You may also check the second example in this[^] MSDN reference page, although I wouldn't like to see it in actual code!)
-
Luc Pattyn wrote:
Absolutely
I agree, in fact this is the perfect example of when a goto may be the most appropriate. It can be a useful statement - it's only because it can be (and has been) wildly misused that it's frowned upon. :-D
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)