They're called braces or curly brackets, not to be confused with the "curly arrow" in some languages, ~>
.
In C, and by inheritance C++, these are optional on single-line if
statements, but as many, many bugs have been created by omitting them you'd be advised to use them as a matter of principle even when they're redundant.
That is a mistake like this is easy to overlook:
if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
goto fail;
goto fail;
Where it seems like that goto
is conditional, yet it's not, it just drops through. This is the huge OpenSSL bug that caught everyone by surprise, and if veteran developers can mess it up, so can you.
The second form is the most reliable, least ambiguous, especially when formatted according to typical conventions:
for (int i = 10 ; b >= i;i++) {
if (i%2 == 0) {
cout << "even" << endl;
}
else {
cout << "odd" << endl;
}
}
for
is a statement, not a function, so the syntax is for (...)
with a space. Functions have no space, like f(...)
. Omitting the space implies for
is a function, which it absolutely is not. The same goes for if
, while
and so on.
It's worth noting that the original code can actually be reduced to:
for (int i = 10 ; b >= i;i++)
if (i%2 == 0)
cout << "even" << endl;
else
cout << "odd" << endl;
Since that if
is a single statement, even with the else
clause attached.
Again, this is not advised because the rules of what is and isn't a single statement can be confusing.