6. 语句
6.1 简单语句
每行都应该只包含一条语句。
6.2 返回语句
一个返回语句不要用最外围圆括号。不用:
return (n * (n + 1) / 2);
用: return n * (n + 1) / 2;
6.3 If, if-else, if else-if else 语句
if, if-else and if else-if else 语句看起来应该像这样:
if (condition) { DoSomething(); ... } if (condition) { DoSomething(); ... } else { DoSomethingOther(); ... } if (condition) { DoSomething(); ... } else if (condition) { DoSomethingOther(); ... } else { DoSomethingOtherAgain(); ... } |
6.4 for / foreach 语句
一个for语句应该如下形式:
for (int i = 0; i < 5; ++i) { ... } |
或者放置一行(考虑用一个while语句代替)
for (initialization; condition; update) ; |