<分区>
if else 语句中的缩进是否对代码的执行有任何影响,还是只是为了让代码更清晰?
书中的例子 Accelerated C++ 由 Andrew Koening 撰写:
while(c != cols) {
if(r == pad + 1 && c == pad + 1) {
cout << greet;
c += greet.size();
} else {
if(r == 0 || r == rows - 1 || c == 0 || c == cols - 1)
cout << "*";
else
cout << " ";
++c;
}
}
无论r=0与否,c
的前缀自增都会执行,但我不明白为什么。
如果if 语句为真,则打印一个星号。 如果不是,则打印一个空格并递增c
。
这就是我阅读它的方式,但是无论 r
或 c
的值是多少,c
都会递增。
书上是这样写的,但我找不到任何解释:
Notice how the different indentation of ++c; draws attention to the fact that it is executed regardless of whether we are in the border.