Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      do-while loop

      From cppreference.com
      <c‎ |language
       
       
       
      Statements
      Labels
      Expression statements
      Compound statements
      Selection statements
      Iteration statements
      do-while
      Jump statements
       

      Executes astatement repeatedly until the value of the conditionexpression becomes false. The test takes place after each iteration.

      Contents

      [edit]Syntax

      attr-spec-seq (optional)dostatementwhile (expression);
      attr-spec-seq -(C23) an optional list ofattributes, applied to the loop statement.
      statement - anystatement, typically a compound statement, which is the body of the loop.
      expression - anyexpression ofscalar type. This expression is evaluated after each iteration, and if it compares equal to zero, the loop is exited.

      [edit]Explanation

      Ado-while statement causes thestatement (also calledthe loop body) to be executed repeatedly until theexpression (also calledcontrolling expression) compares equal to0. The repetition occurs regardless of whether the loop body is entered normally or by agoto into the middle ofstatement.

      The evaluation ofexpression takes place after each execution ofstatement (whether entered normally or by agoto). If the controlling expression needs to be evaluated before the loop body, thewhile loop or thefor 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.

      A program with an endless loop has undefined behavior if the loop has no observable behavior (I/O, volatile accesses, atomic or synchronization operation) in any part of itsstatement orexpression. This allows the compilers to optimize out all unobservable loops without proving that they terminate. The only exceptions are the loops whereexpression is a constant expression;do{...}while(true); is always an endless loop.

      As with all other selection and iteration statements, thedo-while statement establishesblock scope: any identifier introduced in theexpression goes out of scope after the statement.

      (since C99)

      [edit]Notes

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

      [edit]Keywords

      do,while

      [edit]Example

      Run this code
      #include <stdio.h>#include <stdlib.h> enum{ SIZE=8}; int main(void){// trivial exampleint array[SIZE], n=0; do array[n++]=rand()%2;// the loop body is a single expression statementwhile(n< SIZE); puts("Array filled!");    n=0; do{// the loop body is a compound statementprintf("%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 generateint num=1234, i=0;char s[10]; do s[i++]= num%10+'0';// get next digit in reverse orderwhile((num/=10)>0);     s[i]='\0';puts(s);}

      Possible output:

      Array filled!1 0 1 1 1 1 0 04321

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 6.8.6.3 The do statement (p: 156)
      • C17 standard (ISO/IEC 9899:2018):
      • 6.8.5.2 The do statement (p: 109)
      • 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 fordo-while loop
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/do&oldid=183392"

      [8]ページ先頭

      ©2009-2025 Movatter.jp