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) {
}
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.
if(condition1 && !condition2 || (condition3() && ~condition4)) {
}
Else
If you have an else-if the closing bracket and the else goes on the same line. Spaces go after the brackets.
BAD:
}
else{
}else
if(){
}
else
if()
{
GOOD:
} else if() {
} else{
Last updated
Was this helpful?