Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      while loop

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

      Executes astatement repeatedly, until the value ofexpression becomes equal to zero. The test takes place before each iteration.

      Contents

      [edit]Syntax

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

      [edit]Explanation

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

      The evaluation ofexpression takes place before each execution ofstatement (unless entered by a goto). If the controlling expression needs to be evaluated after the loop body, thedo-while loop may be used.

      If the execution of the loop needs to be terminated at some point, break statement can be used as a 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;while(true) is always an endless loop.

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

      (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

      while

      [edit]Example

      Run this code
      #include <stdio.h>#include <stdlib.h>#include <string.h>enum{ SIZE=8};int main(void){// trivial exampleint array[SIZE], n=0;while(n< SIZE) array[n++]=rand()%2;puts("Array filled!");    n=0;while(n< SIZE)printf("%d ", array[n++]);printf("\n"); // classic strcpy() implementation// (copies a null-terminated string from src to dst)char src[]="Hello, world", dst[sizeof src],*p= dst,*q= src;while((*p++=*q++))// double parentheses (that are not strictly necessary)// used to suppress warnings, ensuring that this is an// assignment (as opposed to a comparison) by intention,// whose result is used as a truth value;// null statementputs(dst);}

      Output:

      Array filled!1 0 1 1 1 1 0 0 Hello, world

      [edit]References

      • C17 standard (ISO/IEC 9899:2018):
      • 6.8.5.1 The while statement (p: 109)
      • C11 standard (ISO/IEC 9899:2011):
      • 6.8.5.1 The while statement (p: 151)
      • C99 standard (ISO/IEC 9899:1999):
      • 6.8.5.1 The while statement (p: 136)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 3.6.5.1 The while statement

      [edit]See also

      C++ documentation forwhile loop
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/while&oldid=147751"

      [8]ページ先頭

      ©2009-2025 Movatter.jp