Need help understanding while(!done) loops

This is a discussion on Need help understanding while(!done) loops within the C Programming forums, part of the General Programming Boards category; Hello, i'm relatively a new c programmer,and i bought a book on C and ive read up to while(!done) loops ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    70

    Post Need help understanding while(!done) loops

    Hello, i'm relatively a new c programmer,and i bought a book on C and ive read up to while(!done) loops and then i got confused.I got confused mainly because of the !done part and the structure of the loop, so if you have knowledge of how a while(!done) loop works could you attempt or try helping me understand it? Thank you

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    All you really need to know when thinking about the !done part is that done is true unless it is zero, and NOT flip-flops the result. Is there something else?

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by cuo741 View Post
    Hello, i'm relatively a new c programmer,and i bought a book on C and ive read up to while(!done) loops and then i got confused.I got confused mainly because of the !done part and the structure of the loop, so if you have knowledge of how a while(!done) loop works could you attempt or try helping me understand it? Thank you
    What part of it don't you understand? It's a while loop, which will continue while !done is true. ! is done. So it says, in English, obviously: "while not true". So it basically reads as English..

  4. #4
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    What are you talking about? I'm confused, could you show an example of a "while(!done) loop"?

    A while repeats while the equation in between the parentheses evaluates to anything but 0. For example while(1) would repeat forever because 1 is always not 0.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    70
    Quote Originally Posted by User Name: View Post
    What are you talking about? I'm confused, could you show an example of a "while(!done) loop"?

    A while repeats while the equation in between the parentheses evaluates to anything but 0. For example while(1) would repeat forever because 1 is always not 0.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define FALSE 0
    #define TRUE !FALSE
    #define OMEGA ('Z' - 'A')
    
    int main()
    {
          int done;
          long int r;
          char alpha;
    
          srand((unsigned)time(NULL));
          
          done = FALSE;
          while(!done)
          {
               r = rand() % OMEGA;
               alpha = 'A' + (char)r;
               if(alpha=='Q') done = TRUE;
               putchar(alpha);
          }
          putchar('\n');
          return (0);
    }
    this is the exampe code from the book im reading

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Hmm. Tell me what you think is happening.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    70
    well in the book it shows me that it makes a string of random letters and when it gets to a Q it stops the loop but, the problem i have with understanding this is basically the !done part, thats really the whole problem here is, if you could explain it more throughly or with more detail if possible that would be most appreciated

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Quote Originally Posted by whiteflags View Post
    All you really need to know when thinking about the !done part is that done is true unless it is zero, and NOT flip-flops the result.
    That's really all there is.

    Since you understand that the loop ends when you get a Q, lets look at done there.

    if(alpha=='Q') done = TRUE;

    Cool, so what is TRUE?

    #define FALSE 0
    #define TRUE !FALSE
    Aha.

    So, if we substitute done, we would see while (!0) a lot. But when the loop exits the condition is basically while (!!FALSE). Just remember that NOT flip-flops, and flop FALSE twice to get it.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    70
    Ahhhhhhhh, i get it now, thanks a lot for helping me, im hoping to get into the game programming business but, i hear its very hard so im learning C right now and then moving onto C++, any suggestions on what other languages i should learn?

  10. #10
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Assembly.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    70
    Quote Originally Posted by User Name: View Post
    Assembly.
    Whats assembly? is it a programming language?and what is it for?

  12. #12
    Registered User
    Join Date
    Jun 2010
    Posts
    45
    perl would be a good utility language to pick up

  13. #13
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by cuo741 View Post
    Whats assembly? is it a programming language?and what is it for?
    Assembly is the language that your computer processor understands. Everything you write in other languages such as c or c++ gets translated into assembly after compilation and build. Of course things are a little more complex than they sound.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    In the game programming business you're not going to need assembly (well, maybe once in a while, for certain minor optimizations, but that will be done by the 'experts' I think...). Perl is quite useless as well for game programming. Learn C, C++, maybe Java. And of course DirectX (not a language, but a library) or possibly OpenGL (the former is more common in game industry, so I advise that, even though I prefer OpenGL).

    It can be useful to learn the basics of Assembly language to understand the computer better, but don't go beyond the basics unless you have too much time on your hands.

  15. #15
    Registered User
    Join Date
    Jun 2010
    Posts
    45
    oops, didnt read you want to get into game programming.

    yea, perl wont help much there. c++ is the way to go after you come to terms with C

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with loops - super n00b
    By antipesto93 in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2010, 10:57 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. understanding recursive functions
    By houler in forum C Programming
    Replies: 7
    Last Post: 12-09-2004, 12:56 PM
  4. Evaluation of nested loops
    By Mister C in forum C Programming
    Replies: 2
    Last Post: 08-13-2004, 02:47 PM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21