while循环中的递增和递减
Increment and decrement in while loops
这是我遇到问题的代码类型,只是一个例子:
#include <stdio.h>
#define TEN 10
int main(void)
{
int n = 0;
while (n++ < TEN)
printf("%5d", n);
printf("\n");
return 0;
}
这里,自增运算符先在while循环内部工作,n取1;然后它再次为 while 循环运行 printf() 语句,所以现在 n 得到 2 并且第一次执行 printf() 语句时打印“1”?因为否则,将打印“0”。我不确定它是否确实以这种方式工作,所以你能解释一下吗,我是对的吗?
PS:没有老师,请教各位...
n++
是一个 post
增量。请参阅标准 n1570
部分 6.5.2.4
The result of the postfix ++ operator is the value of the operand. As
a side effect, the value of the operand object is incremented (that
is, the value 1 of the appropriate type is added to it).
while (n++ < TEN) /* 0 < 10 not 1 < 10 */
printf("%5d", n);/* here incremented value gets printed */
这里的技巧是 ++
postfix 运算符:它被称为 post-incrementation。表达式中使用的值如果 n
的值在 增量之前和 n
在下一个序列点之前的某个点更新,或者是结束表达式、函数调用或逗号运算符,您不太可能很快需要的高级运算符。
另一个警告是 C 语言中布尔值的特殊处理:如果 n < 10
为真,则计算结果为 1
,否则为 0
。相反,没有比较的测试(例如 if (n)
对任何非零、非空和非 NaN 值都成功。
事件顺序如下:
int n = 0
定义类型 int
的局部变量 n
并将其初始化为值 0
.
while (n++ < TEN)
: n
与 TEN
比较,结果为真(1
in C)因为 0 < 10
then n
自增得到值 1
。比较的结果为真,因此 while
循环继续执行其命令语句。
printf("%5d", n);
打印 n
的值,即 1
.
- 执行继续进行循环测试。
while (n++ < TEN)
: n
再次与 TEN
比较,结果仍然是 1
since 1 < 10
then n
自增得到值 2
。比较的结果为真,因此 while
循环继续执行其命令语句。
printf("%5d", n);
打印 n
的值,即 2
。请注意,此输出与上一个输出和下一个输出之间没有分隔符。
- 执行继续进行循环测试。
- ...重复这些步骤,直到打印出
9
while (n++ < TEN)
: n
与 TEN
比较,结果是 1
since 9 < 10
then n
递增并得到值 10
。比较的结果仍然为真,因为比较是在递增之前执行的,因此 while
循环继续执行其命令语句。
printf("%5d", n);
打印 n
的值,即 10
.
- 执行继续进行循环测试。
while (n++ < TEN)
:n
与 TEN
比较,结果为假(C 中的 0
),因为 101 is not
< 10**then**
nis incremented and gets the value
11. The result of the comparison is false hence the
while` 循环停止,控制跳到下一条语句。
printf("\n");
打印一个换行符,结束输出行 12345678910
return 0;
main
函数 returns 退出状态为 0
,表示成功。
这是我遇到问题的代码类型,只是一个例子:
#include <stdio.h>
#define TEN 10
int main(void)
{
int n = 0;
while (n++ < TEN)
printf("%5d", n);
printf("\n");
return 0;
}
这里,自增运算符先在while循环内部工作,n取1;然后它再次为 while 循环运行 printf() 语句,所以现在 n 得到 2 并且第一次执行 printf() 语句时打印“1”?因为否则,将打印“0”。我不确定它是否确实以这种方式工作,所以你能解释一下吗,我是对的吗? PS:没有老师,请教各位...
n++
是一个 post
增量。请参阅标准 n1570
部分 6.5.2.4
The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it).
while (n++ < TEN) /* 0 < 10 not 1 < 10 */
printf("%5d", n);/* here incremented value gets printed */
这里的技巧是 ++
postfix 运算符:它被称为 post-incrementation。表达式中使用的值如果 n
的值在 增量之前和 n
在下一个序列点之前的某个点更新,或者是结束表达式、函数调用或逗号运算符,您不太可能很快需要的高级运算符。
另一个警告是 C 语言中布尔值的特殊处理:如果 n < 10
为真,则计算结果为 1
,否则为 0
。相反,没有比较的测试(例如 if (n)
对任何非零、非空和非 NaN 值都成功。
事件顺序如下:
int n = 0
定义类型int
的局部变量n
并将其初始化为值0
.while (n++ < TEN)
:n
与TEN
比较,结果为真(1
in C)因为0 < 10
thenn
自增得到值1
。比较的结果为真,因此while
循环继续执行其命令语句。printf("%5d", n);
打印n
的值,即1
.- 执行继续进行循环测试。
while (n++ < TEN)
:n
再次与TEN
比较,结果仍然是1
since1 < 10
thenn
自增得到值2
。比较的结果为真,因此while
循环继续执行其命令语句。printf("%5d", n);
打印n
的值,即2
。请注意,此输出与上一个输出和下一个输出之间没有分隔符。- 执行继续进行循环测试。
- ...重复这些步骤,直到打印出
9
while (n++ < TEN)
:n
与TEN
比较,结果是1
since9 < 10
thenn
递增并得到值10
。比较的结果仍然为真,因为比较是在递增之前执行的,因此while
循环继续执行其命令语句。printf("%5d", n);
打印n
的值,即10
.- 执行继续进行循环测试。
while (n++ < TEN)
:n
与TEN
比较,结果为假(C 中的0
),因为101 is not
< 10**then**
nis incremented and gets the value
11. The result of the comparison is false hence the
while` 循环停止,控制跳到下一条语句。printf("\n");
打印一个换行符,结束输出行12345678910
return 0;
main
函数 returns 退出状态为0
,表示成功。