For anything else than such short blocks I would indeed recommend curly braces as it increases readability a lot. |
Um, if you want more than one statement in your conditionally-executed block, you
have to use curly braces. It's not a recommendation, it's a necessity. Not because of readability, but because if you don't, only the first statement will be considered to be dependent on the condition. Consider the following:
1 2 3 4 5 6 7 8 9 10 11
|
bool bFlag;
// ...
if (bFlag)
std::cout << "bFlag is true" << std::endl;
else
std::cout << "bFlag is false" << std::endl;
std::cout << "yes, it's definitely false" << std::endl;
std::cout << "End" << std::endl;
|
If bFlag is true, you will get the following output:
bFlag is true
yes, it's definitely false
|
Because the "else" block doesn't use curly braces, only line 8 is part of the else block. Line 9 is not part of the else block, and is not part of the whole if... else... statement - it's always executed regardless of the value of bFlag.
In fact, even with single-line conditional blocks, I'd recommend using curly braces. I've seen so many bugs introduced into code, because somebody didn't use them, and then they (or someone else) went back and wanted to add a second line to the block, but forgot to put the braces in.
You're best off getting into the habit of putting them in, all the time. It's safer that way.