Thread: While(1) loop.

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0

    While(1) loop.


    Hi friends,

    I know that 'while(1)' is an infinite loop. I have noticed that the keyword while(1) is used like under.


    while(1)
    {
    statements
    }

    This means statements within the braces will be executed endlessly.

    What does the below mentioned code mean?

    {
    statements
    while(1);
    }

    Does it also mean that statements will be executed endlessly?

    If so, why this keyword is used in different way?


    regards


    swapan
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    The second fragment executes "statements" once, then loops indefinitely doing nothing (except hogging CPU cycles). The semi-colon in that context is a "null statment" - a do nothing.

    A better idiom for an indefinite loop is:
    Code:
    for(;;)
    {
      // stuff
    }
    Some compilers will issue an "expression is always true" or similar warning for "while(1)" or "while(true)", but will not for "for(;;)".

    As ever, the better way to answer your question would be to a) run the code, b) step the code in your debugger. You should not have got as far as while loops in programming without learning to use a debugger. It will save you having to ask this sort question with answers of varying quality.
    Last edited by clifford; March 26th, 2013 at 07:52 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0

    While(1) loop.


    Thanks clifford for your nice clarification. Actually, I am an electronics hobbyist. I have no institutional background in this field. Presently I am working on microcontroller based small projects. I have developed some codes in assembly language. Since code development in higher level language like C, offers many advantage over assembly and forum members (On Embedded technology) extend assistance in C. That's why I have started to learn C. All that I have is a book on C. Such active help from experts like you will certainly make me go ahead.


    Regards

    swapan


    Originally Posted by clifford
    The second fragment executes "statements" once, then loops indefinitely doing nothing (except hogging CPU cycles). The semi-colon in that context is a "null statment" - a do nothing.

    A better idiom for an indefinite loop is:
    Code:
    for(;;)
    {
      // stuff
    }
    Some compilers will issue an "expression is always true" or similar warning for "while(1)" or "while(true)", but will not for "for(;;)".

    As ever, the better way to answer your question would be to a) run the code, b) step the code in your debugger. You should not have got as far as while loops in programming without learning to use a debugger. It will save you having to ask this sort question with answers of varying quality.

IMN logo majestic logo threadwatch logo seochat tools logo