What good is while(0) and while(1) in C?

Asked

Viewed 9,258 times

16

What is the purpose of this? While(0) or While(1). Does 1 verify that it is correct and 0 that is wrong? Why?

3 answers

20


A loop continues (executes the next step) if its condition is true, and does not continue if it is false. In C, zero is considered "false" and everything else is considered true. So:

  • while(0) means "never enter the loop". Alone, it makes no sense (because it would be easier simply not to have the loop...), but as pointed out by pmg and Renan Cavalieri, it can be useful in a construction do...while.

    Although a block do...while with always false condition will only perform once - and therefore could be replaced by the body of the loop and only - it can be useful to encapsulate two or more instructions in a single macro. Behold the response of the pmg for an example. The utility of this is that the macro can then be used anywhere that expects an instruction, without breaking the syntax or semantics of the code. Another example would be:

    #define foo do { if ( condição ) instrução; } while(0)
    
    if (condition)
        foo;
    else
        bar;
    

    Resulting in:

    if (condition)
        do { if ( condição ) instrução; } while(0);
    else
        bar;
    

    Source

  • while(1) means "always get in the loop". That is, every time the condition is tested (at the beginning, and every time a step ends) it will give true and the loop will continue. That is essentially an "infinite loop", that is, unless in the body of the loop there is an instruction break or return that ends the same.

    The motivation of infinite loops is, among other things, to make a process run continuously while the program is active, with no "end date". An example would be a REPL console ("read-Eval-print loop") which reads a user statement, executes, prints the result and goes to the next, ad infinitum, or until the user enters with a command to exit. Another would be a web server, for example, that waits for a request to arrive, handles it and sends the response to the client, and continues to do so while the server is on (potentially for days, months, even years). There are also other examples such as an OS event handler (listening by mouse clicks and keyboard keystrokes), simulations, games, etc.

  • 1

    The while(0) is usually the final part of a cycle do

  • 2

    I found this link showing some examples of use - http://www.bruceblinn.com/linuxinfo/DoWhile.html

  • 2

    +1, only that I took a little while to understand why the first example would give problem without the while(0)

8

The while(1) is normally used for infinite cycles

while (1) {
    /* ciclo infinito, por exemplo */
    system("stayactive"); // mantem a aplicacao activa, mesmo que ela crashe
}

The while(0) is usually used as the final part of a cycle do to group a number of instructions into a macro and maintain the syntax of C with respect to semicolon

#define XPTO do { um(); dois(); tres(); } while(0)

/* nota o ; abaixo */
XPTO;

1

The C language does not have the Bolean type by default, so it uses 1 and 0 as true/or false, as in binaries, where 1 has voltage and 0 does not. So while - or any other condition command like 0 being false and 1 - or any number other than 0 - is true.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.