arm*_*ali 0 c++ if-statement while-loop c++17 c++20
为什么我可以这样做:
if (int result=getValue(); result > 100) {
}
Run Code Online (Sandbox Code Playgroud)
但不能这样做:
while (int result=getValue(); result > 100) {
}
Run Code Online (Sandbox Code Playgroud)
为什么要歧视while
?条件就是条件。为什么while
不能像if
can那样评价它?
为了使用 实现所需的行为while
,我必须以这种方式实现它:
int result = getValue();
while (result > 100) {
//do something
result = getValue();
}
Run Code Online (Sandbox Code Playgroud)
Bar*_*rry 11
因为我们已经有了一个带有初始化器的 while 循环。它的拼写是:
for (int result=getValue(); result > 100;) {
}
Run Code Online (Sandbox Code Playgroud)