What is the purpose of while (0) and while (1) in C?

15

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

    
asked by anonymous 17.12.2015 / 03:40

3 answers

18

A loop continues (performs 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 to be true. Here's how:

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

    Although a do...while block with always false condition will only execute once - and therefore could be replaced by the body of the loop and only - it may be useful to encapsulate two or more statements in a single macro. See pmg for an example. The usefulness of this is that the macro can then be used anywhere that expects an instruction, without breaking the syntax or code semantics. 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;
    

    Font

  • while(1) means "always enter the loop". That is, every time the condition is tested (at the beginning, and whenever a step finishes) it will give true and the loop will continue. This is essentially an "infinite loop", that is, unless in the body of the loop there is a statement break or return that causes it to terminate.

    The motivation of infinite loops is, among other things, cause a process to run continuously while the program is active, with no "end date". An example would be a REPL (read-eval-print loop) console that reads a user's instruction, executes, prints the result, and goes to next, ad infinitum or until the user enters 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 (listen for mouse clicks and keyboard keystrokes), simulations, games, etc.

17.12.2015 / 05:07
7

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 normally used as the final part of a do cycle to group a number of statements into a macro and maintain the C-syntax for semicolons

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

/* nota o ; abaixo */
XPTO;
    
17.12.2015 / 10:45
1

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

    
18.12.2015 / 00:56