Thread: is this bad to do?

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    26

    is this bad to do?

    i'm just wondering if it is bad to leave loops and then never finish them. For instance, if i was in the game loop, called the main menu, and then called the game loop, without every getting to the end of the game loop.

  2. #2
    Registered User Liam Battle's Avatar
    Join Date
    Jan 2002
    Posts
    114
    did you just ask if its bad to have an infinite loop?

    that is hallarious
    LB0: * Life once school is done
    LB1: N <- WakeUp;
    LB2: N <- C++_Code;
    LB3: N >= Tired : N <- Sleep;
    LB4: JMP*-3;

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    No it is in general not bad to leave a loop without reaching an end condition.

    And to Liam. I don't find it hillarious at all. I actualy been asking myself the same question to sometimes.

  4. #4
    Registered User Liam Battle's Avatar
    Join Date
    Jan 2002
    Posts
    114
    Barjor, you obviously must be new.
    leaving a loop in a infinite process is just bad code.
    LB0: * Life once school is done
    LB1: N <- WakeUp;
    LB2: N <- C++_Code;
    LB3: N >= Tired : N <- Sleep;
    LB4: JMP*-3;

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Wind your neck in Liam. m00se123 is probably new to the coding game and has asked a perfectly valid question. Get off your high horse.

    leaving a loop in a infinite process is just bad code
    That sentence is just bad english. Don't you mean "Leaving a process in an infinite loop"?

    Writing a loop with no exit condition is perfectly valid.
    Code:
    while(1)
    {
       // do something
       if(something && somethingelse)
       {
          break;
       }
    }
    There is nothing wrong with doing that.

    However, creating a loop and having NO way whatsoever to exit the loop is bad. You'll figure out why soon enough. Your program just will not finish running, and you'll need to kill it some other way.

    Hope this helps
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  6. #6
    Registered User Liam Battle's Avatar
    Join Date
    Jan 2002
    Posts
    114
    Uraldor, hey ***got, dont get into someone's business u dumb ****... im not talking to the original poster anyhow, so learn how to follow post's...

    and leaving an infinite loop is bad code.

    and english skills is not important in coding bud... so goto a dictionary / grammer forum if you want to spread that ****..
    LB0: * Life once school is done
    LB1: N <- WakeUp;
    LB2: N <- C++_Code;
    LB3: N >= Tired : N <- Sleep;
    LB4: JMP*-3;

  7. #7
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    does it work? if yes, do it. if no, don't do it.
    if it looks nice and will work in 99.9999% of all cases, do it.

    imho,
    Code:
    for ( ; ; ) {
    ...
    }
    is more logical than any equivalent

  8. #8
    Registered User Liam Battle's Avatar
    Join Date
    Jan 2002
    Posts
    114
    well think of the effects of an infinite loop.

    then ask yourself is this good ?

    why people have to get all hot and bothered about it is a question for the DH.

    btw ygfperson your qoute rox
    LB0: * Life once school is done
    LB1: N <- WakeUp;
    LB2: N <- C++_Code;
    LB3: N >= Tired : N <- Sleep;
    LB4: JMP*-3;

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    26
    i don't know if i mentioned that my prog does end, it just doesn't ever make to the end. i used the exit function to get out, and before i call that i call my own function to clean everything up. It is not like i just have an infinite loop.

  10. #10
    Registered User DeadArchDown's Avatar
    Join Date
    Apr 2002
    Posts
    28
    The guy never mentioned an infinite loop. You infered that. From what he posted...that was a poor inference Liam. If you actually read it, the logical inference is that the loop is not infinite....just that it exists the loop early.
    -------------------
    "Exception"

  11. #11
    Registered User Liam Battle's Avatar
    Join Date
    Jan 2002
    Posts
    114
    Actually it implies the loop is infinite bud, read it back.. it never says he exits the program or the function early...

    in either case both are not recommended, the infinite loop is obviously not a good idea, and a break or exit() function are not recommended, because loops and program logic is suggested to be 1 entry point and 1 exit point.

    DeadArchDown please read who is the original poster...
    LB0: * Life once school is done
    LB1: N <- WakeUp;
    LB2: N <- C++_Code;
    LB3: N >= Tired : N <- Sleep;
    LB4: JMP*-3;

  12. #12
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    It will not kill your code but I was always taught, one entry, one exit for every function. Granted that some functions don't lend themselves well to this but if you take some time you can usually follow this rule if you re-structure your code.




    Initialize();

    //Game/app loop
    while (PlayingGame)
    {
    //Play the game
    }

    //player/user exited game/app if we get here
    Cleanup();


    So when someone exits the game, we would set the PlayingGame variable to 0 and thus exit the main loop. Always try to remember:

    One entry
    One exit

    If you make a habit of exiting from functions helter skelter like, your code will be very hard to read and the more you jump around, the slower it will be.

    I am usually able to follow the one entry one exit rule with only a very few rare exceptions. If you want to break out of a loop just set a loop variable to test by and use that to break out of the loop, whether it be a local loop or the main code loop.

    Some make a case for goto to exit out of certain loops but I've always been able to code (in C) functions and loops in such a manner that I do not need a goto. There is a huge debate on the infamous goto and its use and practicality in modular programs. Personally, I stay away from it because that's what I was taught. I had to purposely structure my code so that it would have one entry, one exit, one main purpose per function, and no gotos. That's what my instructors taught me and that's what I've done ever since.

    So never completing loops will not crash your code, unless you are have designed it to fall through to free a pointer or something in which case exiting would cause the pointer to never be freed.

    All of the local variables used in loops go out of scope when you exit anyways and then when you exit the function. C pops everything off of the stack as though it never existed and returns to the caller.

  13. #13
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Liam, I think you're reading too much into the original post. And your reply was kind of harsh for someone new (especially if you don't fully understand what he/she is asking).

    Here's what I get from moose's post...

    Code:
    //game loop
    while(GameState==LoopOne)
    {
        if(ClickedOnBtn1()) shoot();
        if(ClickedOnBtn2()) 
        {
             GameState=LoopTwo;
             break;
        }
        if(ClickedOnBtn3())
        {
            GameState=LoopThree;
            break;
        }
        if(ClickedOnBtn4()) run();
    }
    
    //main menu loop
    while(GameState==LoopTwo)
    {
       //stuff in loop 2.
    }
    If function ClickedOnBtn2 returns true then you reset the GameState variable and exit the loop. Granted the code I provided is really generic, but from what I read of the original post that's a basic idea of what he's saying. If a certain condition is met you move on to another loop without ever completing the rest of the current loop. He never said infinite. He never said his loop didn't end. He said:

    without every getting to the end of the game loop.
    I think you need to re-read the original post.

    And this crap..

    Uraldor, hey ***got, dont get into someone's business u dumb ****...
    is completely uncalled for. Uraldor was simply telling you to go easy on a new poster.
    Last edited by jdinger; 05-20-2002 at 08:59 PM.

  14. #14
    Registered User Liam Battle's Avatar
    Join Date
    Jan 2002
    Posts
    114
    haha what is this a carebear forum?

    "this was un called for" hahahaha...

    there is a tissue on the desk there for ya... to the left a little bit..
    LB0: * Life once school is done
    LB1: N <- WakeUp;
    LB2: N <- C++_Code;
    LB3: N >= Tired : N <- Sleep;
    LB4: JMP*-3;

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    It is NOT bad style to leave a loop before it has reached the endpoint. So long as one takes care to deallocate memory local to that loop, close open files, etc...

    well think of the effects of an infinite loop.
    ...um, ok, it's called a non-terminating program...(or one that terminates violently, sometimes).

    Liam: you should really take it easy. This is a message board, not a kiddie chat
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bad and fail of steam
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 02-19-2008, 03:07 AM
  2. Can you still view the bios screen with a bad CPU?
    By HyperCreep in forum Tech Board
    Replies: 4
    Last Post: 12-31-2006, 06:57 PM
  3. data loss bad bad bad
    By RoD in forum Tech Board
    Replies: 4
    Last Post: 05-01-2003, 12:06 PM
  4. Shocking(kind of)
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 12-10-2002, 08:52 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM