Namespaces
Variants
Views
Actions

do-while loop

From cppreference.com
< c‎ | language

Executes a statement repeatedly until the value of condition becomes false. The test takes place after each iteration.

Contents

[edit] Syntax

do statement while ( expression ) ;
expression - any expression of scalar type. This expression is evaluated after each iteration, and if it compares equal to zero, the loop is exited.
statement - any statement, typically a compound statement, which is the body of the loop

[edit] Explanation

A do-while statement causes the statement (also called the loop body) to be executed repeatedly until the expression (also called controlling expression) compares equal to 0. The repetition occurs regardless of whether the loop body is entered normally or by a goto into the middle of statement.

The evaluation of expression takes place after each execution of statement (whether entered normally or by a goto). If the controlling expression needs to be evaluated before the loop body, the while loop or the for loop may be used.

If the execution of the loop needs to be terminated at some point, break statement can be used as terminating statement.

If the execution of the loop needs to be continued at the end of the loop body, continue statement can be used as a shortcut.

As an extension to the as-if rule, a loop with no side effects (I/O, volatile accesses, or atomic synchronizations) in any part of its expression or loop_statement may be optimized out even if it is an endless loop. The only exceptions are the loops where expression is a constant expression; do {...} while(true); is always an endless loop.

As with all other selection and iteration statements, the do-while statement establishes block scope: any identifier introduced in the expression goes out of scope after the statement.

(since C99)

[edit] Notes

Boolean and pointer expressions are often used as loop controlling expressions. The boolean value false and the null pointer value of any pointer type compare equal to zero.

[edit] Keywords

do, while

[edit] Example

#include <stdio.h>
#include <stdlib.h>
enum { SIZE = 8 };
int main(void)
{
    // trivial example
    int array[SIZE], n = 0;
    do array[n++] = rand() % 2; // the loop body is a single expression statement
    while(n < SIZE);
    puts("Array filled!");
    n = 0;
    do { // the loop body is a compound statement
        printf("%d ", array[n]);
        ++n;
    } while (n < SIZE);
    printf("\n");
 
    // the loop from K&R itoa(). The do-while loop is used
    // because there is always at least one digit to generate
    int num = 1234, i=0;
    char s[10];
    do s[i++] = num % 10 + '0'; // get next digit in reverse order
    while ((num /= 10) > 0);
    s[i] = '\0';
    puts(s);
}

Possible output:

Array filled!
1 0 1 1 1 1 0 0
4321

[edit] References

  • C11 standard (ISO/IEC 9899:2011):
  • 6.8.5.2 The do statement (p: 151)
  • C99 standard (ISO/IEC 9899:1999):
  • 6.8.5.2 The do statement (p: 136)
  • C89/C90 standard (ISO/IEC 9899:1990):
  • 3.6.5.2 The do statement

[edit] See also

C++ documentation for do-while loop