# C++ Syntax

## Recommended Posts

Plasmator    208
Hey, as you all know, when you're doing a while / for-loop or comparison, if your "action" fits in a single statement then you don't need the braces around that block. For example, if(...) a = b; However, when you need to do more then one thing, you add {}'s: if(...) { a = b; b = c; } I've noticed that you can also do this to multiple statements: if(...) a = b, b = c; My questions are: (I'm referring to the last example with the use of a comma) a)will this syntax work on any modern C++ compiler? b)is this valid? Thanks in advance guys!

##### Share on other sites
Guest Anonymous Poster
perfectly valid

##### Share on other sites
Plasmator    208
Thank you very much for such a quick response! :D

One more qustion, is it recommended style-wise to use this method?

Thanks again.

##### Share on other sites
CTar    1134
Quote:
 Original post by PlasmatorThank you very much for such a quick response! :DOne more qustion, is it recommended style-wise to use this method?Thanks again.

No, it's not recommended. Why would you prefer to seperate statements by commas instead of just putting it in a block?

##### Share on other sites
Agony    3452
I would say that it is most definitely not recommended. Not everyone is familiar with the details of the comma operator (or that there even is a comma operator), and could thus make the code more confusing or misleading. Also, I'm too lazy to think hard about it, but I wouldn't be surprised if there could be some interesting and hard-to-track down bugs caused by using a comma. It isn't identical to using multiple statements inside braces. It's just that it can be used to do something similar in this case.

##### Share on other sites
rip-off    10976
Quote:
 Original post by PlasmatorThank you very much for such a quick response! :DOne more qustion, is it recommended style-wise to use this method?Thanks again.

No, Your code will be unreadable to anyone who doesn't follow the same convention. Not all c/c++ programmers are aware of exactly how the comma operator works.

I personally use braces in if blocks with a simgle statement. It's very clear to me what's going on then, and I can add or remove such statements with liberty without needing to change the braces.

##### Share on other sites
Plasmator    208
Quote:
Original post by CTar
Quote:
 Original post by PlasmatorThank you very much for such a quick response! :DOne more qustion, is it recommended style-wise to use this method?Thanks again.

No, it's not recommended. Why would you prefer to seperate statements by commas instead of just putting it in a block?

I guess it's just my taste... :(
I just wanted to know if alot of people used this syntax! - Doesn't seem so...

Thanks for your replies :)

##### Share on other sites
Guest Anonymous Poster

##### Share on other sites
Just to be safe with ALL compilers, I put brackets around even 1 line statements, not too annoying to code, and helpful when reading.

##### Share on other sites
xiuhcoatl    282
I agree with the above posts - In one case, auditing some code for a client, I saw something similar to this:

if( a<b );    modifyValues();

where the modifier of the code tried to refactor correctly but accidently tossed the ';' at the end of the if statement. Going through the change management system before this change was submitted was a complicated series of tests that had commas, braces/no braces, etc.. which was difficult to read. So you could almost see that the maintainer just C&P'd the block into a function and left what remains above accidently. This incidently led to an integer overflow which created a memory allocation that could, in a very difficult way, be leveraged into an exploitable buffer overflow.

Ultimately I wouldn't say dont ever do it, C/C++ can be very terse and at times those language features are very useful. Just do what makes your code the most readable and conveys clearly what you are trying to "speak" in C/C++.

Hope that helps..

#dth-0

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account