Thread: when a while loop will stop ?

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    92

    when a while loop will stop ?

    what is the condition for the while loop to be invalid ?

    i think the following are the condition for invalid while loop.


    1. while(false ) // bool false --> loop will stop


    2. while(NULL) ----> loop will stop

    3. while(0) ----->loop will stop

    4. while('\0') -----> loop will stop.


    i think there are many more conditions like above... so there are basically different rules.

    is there any other condition which can make the while loop stop ?
    blue_gene

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Anything that reduces to a boolean false will stop a while loop, thus...

    while ( 1 == 2 )

    ... will do. The other examples you give, (some of which may or may not work with all compilers), are simply examples of this.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    The other examples you give, (some of which may or may not work with all compilers), are simply examples of this.
    can u tell which are the doubtfill examples ? i have not found those in the book. looking some codes i have listed out those conditions.
    blue_gene

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    The while loop is pretty simple. If the expression results in any non-zero value, it will continue. If it results in zero, then it will end. Thats all there is to it. There are no doubtful exceptions. If its zero, it stops, period.

  5. #5
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Don't forget you can also use break; to end the loop
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Or a goto or a return or an exit().

  7. #7
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    well... I don't think anyone should use goto, it messes up the code.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    Yea, I'm not even sure how, but I've always thought that 'goto' is bad coding.

    you can make a while loop run for a few cycles then stop. IE:
    Code:
    int j = 1;
    while (j <= 10)
     {
      printf("The loop has cycled %d time//s", j);
      j++;
     }
    The loop will run 10 times. It adds 1 to j each cycle until J is greater than 10 and the condition is false (which returns 0).

    can u tell which are the doubtfill examples ?
    Yea,
    while(false) wont work because it will look for a variable named false.
    while(NULL) wont work because it will look for a constant called NULL.

    I hope this has helped.
    -=Wynter=-

    PS: I know I've been coding C to long because while typing this, I puntuated a sentince with a semicolon rather than a period. =]

  9. #9
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    Exclamation

    you can make a while loop run for a few cycles then stop
    It's nice when you know for how long it should loop. But what if you don't know. You're then using an endless loop, an endless loop that you only want to stop if something fails...

    You have two "normal" ways to do that:

    the first is by using an extra variable to know when to stop:
    Code:
    int stop=0;
    
    while (!stop) {
       if (something1()==NULL)
          stop = 1;
       else if (something2()==NULL)
          stop = 1;
    
       . . .
    }

    and the second way (the better way, if you ask me), by breaking the while loop:
    Code:
    while (1) {    // or for (;;), same thing.
       if (something1()==NULL)
          break;
       if (something2()==NULL)
          break;
    
       . . . 
    }
    as you can see, with break you save a variable, and the code looks much better.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    More correctly it will look for an object named false or NULL which of course could be anything.

    Things can be doubtful only if you don't look at it in context.

    Code:
    while ( !IamATrueReturingFunction() );
    Now this might look like it would never go into the loop but if the function was actually
    Code:
    int IamATrueReturingFunction(void){return 0;}
    then it becomes a theoritical infinate loop.

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    while (1) {    // or for (;;), same thing.
       if (something1()==NULL)
          break;
       if (something2()==NULL)
          break;
    
       . . . 
    }
    Or you can be a little smarter and do:
    Code:
    while ( something1() != NULL && something2() != NULL ) {
       . . . 
    }
    While breaks are very nice a lot of times they can be used for poor structuring.

  12. #12
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    But but if it's something like that:
    (and I don't want to put it all in a single function and then call only that function.)

    Code:
    while ( something1(var1, var2, var3, var4) 
                && something2(var1, var2, var3, var4) 
                && something3(var1, var2, var3, var4) 
                && something4(var1, var2, var3, var4) 
                && something5(var1, var2, var3, var4) )
    {
    ...
    }
    nice mess, don't you think.


    While breaks are very nice a lot of times they can be used for poor structuring
    define poor structuring.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    define poor structuring.
    IMO using an infinte loop and breaks.
    nice mess, don't you think.
    Not really. Seen much much much worse.

    I was reading Stroustrup's book and he gave his personal opinon that he doesn't like do while loops because then you don't know he exit condition when you start the loop. While I don't agree 100% with this, I do believe that the exit condition(s) should be fairly clear in inside the while() or for(). IMO breaks (in loops) should be used for the "oh crap" situations or when you need to break out of the loop in the middle of the loop. If the breaks are at the beginning or the end of the loop then there is a problem.

  14. #14
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    I do agree that using endless loops is not the best idea, but sometimes it's the only one... I remember writing vga dos games in highschool (don't ask why ) and I had to use an endless loop as the main loop for the game.


    Also, I usually only use break when I deal with endless loops and the switch() statement.

    but I still don't see what's so wrong with using break everytime I want to stop a loop?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  2. need help, fgets won't stop while loop
    By Enkid in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 07:15 AM
  3. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  4. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM
  5. 2 largest elements; -1 to stop loop
    By Peachy in forum C Programming
    Replies: 4
    Last Post: 09-16-2001, 05:16 AM