Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-30-2003, 02:07 PM   #1 (permalink)
agh
Registered User
 
Join Date: Apr 2003
Posts: 30
agh is on a distinguished road
Breaking out of external loop

In one of my programs, I have code like this:

Code:
void f()
{
    //...
    for (/* blah blah*/)
    {
        switch (/* blah blah */) {
            case X:
                //do stuff...
            case Y:
                //do stuff...
             //...
        }
    }
    //...
}
In some cases inside the switch statement, I want the program to get out of the for loop and continue in the function. What's a good way to do this?

I tried this:

Code:
void f()
{
    //...
    bool quit = false;
    for (/* blah blah*/)
    {
        if (quit) {
            //some code...
            break;
        }
        switch (/* blah blah */) {
            case X:
                //do stuff...
                quit = true;
                break;
            case Y:
                //do stuff...
             //...
        }
    }
    //...
}
But if case X is found at the last iteration of the loop, then the "some code..." part inside the if statement won't be reached because the program will break out of the loop and won't check wether quit == true.

Then I did this:

Code:
void f()
{
    //...
    class Stop_Loop {};
    for (/* blah blah*/)
    {
        switch (/* blah blah */) {
            case X:
                //do stuff...
                throw Stop_Loop();
            case Y:
                //do stuff...
             //...
        }
    }
    catch (Stop_Loop) {};
    //...
}
And it works fine. But I want to know if there's a better way than using exceptions to do this. Maybe it's really easy to do but I haven't come up with another way yet.
agh is offline   Reply With Quote
Old 04-30-2003, 02:35 PM   #2 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
Code:
for(x)
top_of_for: {
  switch(j){
       case 1:
          break top_of_for;
   }
}
i tink that will work

or something similiar

i'm *sure* someone will correct me if im wrong however
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 04-30-2003, 03:40 PM   #3 (permalink)
palin
Code Monkey
 
palin's Avatar
 
Join Date: Jan 2003
Posts: 57
palin is on a distinguished road
put the condition check with the for statement thats the way I would do it and have done it.
palin is offline   Reply With Quote
Old 04-30-2003, 04:22 PM   #4 (permalink)
agh
Registered User
 
Join Date: Apr 2003
Posts: 30
agh is on a distinguished road
A condition check inside the for statement won't work. There are too many different cases where the loop needs to be broken. I can't put all of that inside for().
Besides, some code needs to get executed before breaking out of the loop on each case.

Thanks for the help, though.
agh is offline   Reply With Quote
Old 04-30-2003, 04:35 PM   #5 (permalink)
agh
Registered User
 
Join Date: Apr 2003
Posts: 30
agh is on a distinguished road
abc123:

Thanks, I'll try that.
agh is offline   Reply With Quote
Old 04-30-2003, 04:55 PM   #6 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
no, it doesn't work ..

could always do this:

Code:
for ( x ) {
  switch(z){
    case x:
       goto abc;
   }

}

abc:
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 04-30-2003, 07:53 PM   #7 (permalink)
alpha
Regular Contributor
 
Join Date: Feb 2003
Posts: 120
alpha is on a distinguished road
Send a message via AIM to alpha
maybe putting the

if(quit)

stuff under the switch statement...?
alpha is offline   Reply With Quote
Old 04-30-2003, 08:03 PM   #8 (permalink)
agh
Registered User
 
Join Date: Apr 2003
Posts: 30
agh is on a distinguished road
Quote:
Originally posted by alpha
maybe putting the

if(quit)

stuff under the switch statement...?
Of course! I'm so dumb heh... :S
agh is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
While loop not working alemily PHP 2 06-25-2004 12:07 PM
using external content sde PHP 4 12-20-2002 03:02 PM
Loop in scripts causing Crashs mixingsoup PHP 2 12-01-2002 08:37 PM
the endless loop loop loop loop loop loop ... abc123 All Other Coding Languages 2 08-14-2002 03:43 PM


All times are GMT -8. The time now is 11:26 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting