Thread: exiting loops with ease?

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    16

    Question exiting loops with ease?

    I have a whole 1770 lines written of my game, and the character creation process is almost done. However, I am sitting in the middle of a couple of loops and have a small idea on how to get to where I want, but would like to do it as easily as possible.

    Here is what I have:

    while(var1>0)
    {
    ...stuff...
    if(var2==var3)
    {
    while(var1>0)
    {
    ...stuff...
    if(var2==var4)
    {
    ...stuff...
    ...me stuck...
    ...stuff...
    }
    }
    }
    }

    Anyhow, I need to get from where I am stuck to the first while loop, and it seems that if I use break, i break from all of the loops im in. I wouldn't mind using any method to get there, I suspect I'll end up manipulating variable1 for a second to get out of the second while, but don't know how easily that will go. Any ideas? And anyone with a nice small game, how many lines of code do you have before its linked to anything you didn't write?
    "Practice means good, Perfect Practice means Perfect"

  2. #2
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Code:
    #include<stdio.h>
    int main()
    {
    	int i = 0;
    
    	while(true)
    	{
    		while(true)
    		{
    			puts("Inner");
    			break;
    		}
    		puts("Outer");
    		if(++i == 10) break;
    	}
    	return 0;
    }
    If you look at the behaviour of this program you will notice that the inner loop uses a 'break;' statement. What happens though is that the outer loop causes the inner loop to repeat. See the output. The break does not break both loops. In order to break both loops you would have to set a flag in the inner loop, than verify it in the outerloop.
    I compile code with:
    Visual Studio.NET beta2

  3. #3
    V
    Guest

    Re: exiting loops with ease?

    Originally posted by KingRuss
    I have a whole 1770 lines written of my game, and the character creation process is almost done. However, I am sitting in the middle of a couple of loops and have a small idea on how to get to where I want, but would like to do it as easily as possible.

    Here is what I have:

    while(var1>0)
    {
    ...stuff...
    if(var2==var3)
    {
    while(var1>0)
    {
    ...stuff...
    if(var2==var4)
    {
    ...stuff...
    ...me stuck...
    ...stuff...
    }
    }
    }
    }

    Anyhow, I need to get from where I am stuck to the first while loop, and it seems that if I use break, i break from all of the loops im in. I wouldn't mind using any method to get there, I suspect I'll end up manipulating variable1 for a second to get out of the second while, but don't know how easily that will go. Any ideas? And anyone with a nice small game, how many lines of code do you have before its linked to anything you didn't write?


    Well, I can't say for sure without seeing what you're up to, but if you've gotten 4 levels deep into nested conditionals, you're probably not organizing the code very well. ESPECIALLY when two of your conditions are the same, it smacks of poor loop organization.

    I can't think of a case where it WOULD be a good idea to have a loop, nested in another loop, whose condition is the same as the main loop's. I am positive you can rewrite this to minimize the level of nesting of conditionals, and I'd wager that if you set the code up in a more logical manner, you'd find your problems would go away.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    16
    Oh I know that I could "optimize" my code and get it going, or at least looking all the better, but I am not going to. The problem has been long since solved...thanks for your continued assistance though...
    "Practice means good, Perfect Practice means Perfect"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. loops with incrementing strings .... ?
    By twomers in forum C++ Programming
    Replies: 1
    Last Post: 12-12-2005, 11:29 AM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  5. for loops - newbie q's
    By Narciss in forum C Programming
    Replies: 8
    Last Post: 09-26-2001, 02:44 AM