March 26th, 2013, 06:15 AM
I know that 'while(1)' is an infinite loop. I have noticed that the keyword while(1) is used like under.
This means statements within the braces will be executed endlessly.
What does the below mentioned code mean?
Does it also mean that statements will be executed endlessly?
If so, why this keyword is used in different way?
March 26th, 2013, 07:49 AM
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:
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.
March 26th, 2013, 10:36 PM
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.
Originally Posted by clifford