小编Mat*_*att的帖子

C#While(i ++ ...)何时递增?

为什么在检查条件之后而不是在整个循环之后,While循环后递增(或后递减)变量?

如:

int x = 0;
while (x++ < 5)
Console.WriteLine(x);
Run Code Online (Sandbox Code Playgroud)

输出是:1 2 3 4 5,当我认为它应该是0 1 2 3 4.它似乎它立即检查条件 - 真实 - inrements.这是正常的行为,因为我最近一直在练习普通C,这完全不同.

我是否应该更加明确地使用此选项?

while (x < 5)
Console.WriteLine(x);
++x; 
Run Code Online (Sandbox Code Playgroud)

c# loops while-loop post-increment

-7
推荐指数
1
解决办法
2960
查看次数

标签 统计

c# ×1

loops ×1

post-increment ×1

while-loop ×1