非常教程

C参考手册

C 语法

while loop

重复执行语句,直到表达式的值变为零。测试发生在每次迭代之前。

句法

while(表达式)语句

表达

-

标量类型的任何表达式。该表达式在每次迭代之前进行评估,如果它比较等于零,则退出循环。

声明

-

任何语句,通常是复合语句,作为循环的主体

说明

while语句导致语句(也称为循环体)时要执行多次,直到表达(也称为控制表达)进行比较等于零。无论循环体是正常输入还是跳转到语句中间,都会发生重复。

表达式的评估发生在每次执行语句之前(除非由goto输入)。如果需要在循环体之后评估控制表达式,则可以使用do-while循环。

如果循环的执行需要在某个时候终止,break语句可以用作终止语句。

如果需要在循环体的末尾继续执行循环,则可以使用continue语句作为快捷方式。

如果循环在其语句或表达式的任何部分中没有可观察的行为(I / O,易失性访问,原子操作或同步操作),则具有无限循环的程序具有未定义的行为。这允许编译器优化所有不可观察的循环,而不会证明它们终止。唯一的例外是表达式是常量表达式的循环; while(true)总是一个无止境的循环。

与所有其他选择和迭代语句一样,while语句可以建立块范围:表达式中引入的任何标识符在语句后超出范围。

(自C99以来)

笔记

布尔和指针表达式经常用作循环控制表达式。false任何指针类型的布尔值和空指针值都等于零。

关键词

while.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { SIZE = 8 };
int main(void)
{
    // trivial example
    int 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++)
        ; // null statement
    puts(dst);
}

输出:

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

参考

  • C11标准(ISO / IEC 9899:2011):
    • 6.8.5.1 while语句(p:151)
  • C99标准(ISO / IEC 9899:1999):
    • 6.8.5.1 while语句(p:136)
  • C89 / C90标准(ISO / IEC 9899:1990):
    • 3.6.5.1 while语句

C 语法相关

1.#define directive
2.#elif directive
3.#else directive
4.#endif directive
5.#error directive
6.#if directive
7.#ifdef directive
8.#ifndef directive
9.#include directive
10.#line directive
11.#pragma directive
12.alignas
13.Alternative operators and tokens
14.Analyzability
15.Arithmetic operators
16.Arithmetic types
17.Array declaration
18.Array initialization
19.ASCII Chart
20.Assignment operators
21. types
22.Basic concepts
23.Bit fields
24.break statement
25.C language
26.C Operator Precedence
27.cast operator
28.character constant
29.Comments
30.Comparison operators
31.compound literals
32.Conditional inclusion
33.Conformance
34.const type qualifier
35.Constant expressions
36.continue statement
37.Declarations
38.do-while loop
39.Enumerations
40.Escape sequences
41.Expressions
42.External and tentative definitions
43.File scope
44.floating constant
45.for loop
46.Function declarations
47.Function definitions
48.Functions
49.Generic selection
50.goto statement
51.Identifier
52.if statement
53.Implicit conversions
54.Increment/decrement operators
55.Initialization
56.inline function specifier
57.integer constant
58.Lifetime
59.Logical operators
60.Lookup and name spaces
61.Main function
62.Member access operators
63.Memory model
64.Objects and alignment
65.Order of evaluation
66.Other operators
67.Phases of translation
68.Pointer declaration
69.Preprocessor
70.restrict type qualifier
71.return statement
72.Scalar initialization
73.Scope
74.sizeof operator
75.Statements
76.static assert declaration
77.Static storage duration
78.Storage-class specifiers
79.string literals
80.Struct and union initialization
81.Struct declaration
82.switch statement
83.Thread storage duration
84.Type
85.Type
86.Typedef declaration
87.Undefined behavior
88.Union declaration
89.Value categories
90.Variadic arguments
91.volatile type qualifier
92._Alignof operator
93._Noreturn function specifier
C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。