If statements
Control statements like if , else if, for, while, switch, and else statements always have curly brackets on separate lines. Even if they are one line statements.
C++17 adds if initalizers. Similar to for loops, there should be a space in between the initializer and condition.
if(int foo = 5; foo != 4) {
}You can configure your Eclipse to apply this formatting whenever you press Ctrl Shift R
BAD:
if(condition) code();
if(condition) { code(); }GOOD:
if(condition) {
code();
}Logical Operators
When using logical operators such as && and || always put a space in between the operands. Single operand operators such as !, -, or ~ don't have a space.
See more details in the operators section.
Also, the same rules specified on the Line Length page apply to if statements.
Else
If you have an else-if the closing bracket and the else goes on the same line. Spaces go after the brackets.
BAD:
GOOD:
Last updated
Was this helpful?