Line Break means something in C/C++?
I´m merging two branches of a software, and in the process of merging, some code stayed like that:
i开发者_JAVA技巧f (b_flag)
DoSomething();
//MERGE
else
//ENDMERGE
DoOtherThing();
See the disconnection of the if/ else block ? This works like supposted to do ? Compilers take line breaks in consideration ?
Yes, it will work like it's supposed to. When you have an if
or else
with no curly braces ({}
), the next statement is treated as being the body of the if. Inserting line breaks or comments doesn't change the fact that DoSomething(); or DoOtherThing(); are these statements.
Even though comments are effectively handled like whitespace, so it will work like it's "supposed to do". However, you probably should add curly braces to this code before something goes horribly wrong.
Eventually, with the extra comments, a person will miss the else statement, or worse yet a extra statement will get added to the "if block" and the else statement will suddenly become an else statement for some higher nesting if statement which lacks an else.
A good style is to enforce the mandatory inclusion of curly braces in all but the most simple of cases.
A newline is not any different than a space or a tab. Comments are not considered statements, by the way -- they are simply ignored by the compiler.
Edit: The only time a newline matters is inside a C preprocessor directive, such as #define
.
This works exactly as if braces were used.
精彩评论