Reputation: 10536
Let's say we have this C code snippet:
if (condition)
x = a;
else
x = b;
Is it allowed to insert comments like this, without changing the semantics of the code:
if (condition)
/* blah blah blah */
x = a;
else
x = b;
(if there were curly braces, the answer would be obviously yes, but what about these cases of if statements without curly braces?)
Upvotes: 1
Views: 1366
Reputation: 51
Yes you can add comments as u wish.Compiler simply ignores multi-line as well as single line comments comments
Upvotes: 2
Reputation: 84
Yes, the comments are not considered in the compilation and, therefore, does not change the semantics of your code.
Upvotes: 2
Reputation: 11927
Comments have no effect on the code other than the fact that they help to understand and edit code later.
The code you have shown is valid.
If the if
statement is followed by codes inside curly braces all the codes inside the brace will get executed if the condition for if
is met.
If there is no curly braces to group the code, the statement immediately after the if
statement gets executed. If there is comments before this statement it will not effect the code as the comments will be removed when the code is compiled.
Upvotes: 3
Reputation: 781300
Yes. Comments are simply ignored and can be put anywhere that whitespace is allowed.
But I strongly urge you not to write if
statements without curly braces. See Why is it considered a bad practice to omit curly braces?
Upvotes: 2