+6

When should I use curly brackets in C++?

I just recently ran this code on VS and I noticed that it worked fine, even though it didn't use curly brackets! for (int i = 0; i < 10; ++x) { if (i > 5) std::cout << i << "\n"; } Now I know that curly brackets are not necessary if there is only one statement in a for loop or if statement, but I have 2 statements in the for loop. What are the limits and limitations to using curly brackets?

10/21/2019 1:26:40 AM

Dynamic Squid

30 Answers

New Answer

+7

If it's just for the fancy, I'd prefer to choose this style instead: if(1) { { { { printf("%d", 1); } } } } // ;-D

+10

Some recommend using them all the time.

+9

Hi Dynamic Squid. I use curly brackets most of the time. I need them especially when I have more than one instruction, within the condition. For clarity as well.

+6

Also, it helps to make your code more readable, organized, easier to debug.

+6

I would prefer to not use them, if I don't need them, to make the code shorter, but that means I'll have to add them manually and take them out each time I decide I need more than 1/only 1 argument after all, so it's probably more convenient to just always write them. Or you switch to Python, where you don't need them at all. 😜

+5

For single line statement no need to use. like in your code you have single line statement in if condition so Doesn't matter to use of Curly brackets. But for multiline it's necessary like for loop.

+5

Dynamic Squid, to say that the reason for the existence of curly brackets is to be able to write multiple statements in a single line might be stretching it a bit.

+4

Ah, okay. So it's not really anything life changing or anything? Oh, and I actually don't use brackets since the code looks better in my sense :)

+4

Just make sure that if the code you write is to be maintained by others, especially those coming from a Python background that if they add more statements under the if condition, they don't forget to add the curly braces. If not, their logic will be wrong.

+4

HonFu that's a very attractive piece of code 😁. Love the semicolon in the last comment as well 👍

+4

Arpan yes.

+3

if you want to do fancy look of your code then you can use the curly brackets for the single statement also 😀

+3

AJ || ANANT || AC || ANANY || AY that's the hats off performance And yes you already know I have use @.😀

+2

I use curly brackets whenever there is more than one line of code after the specified statement(for, while, etc.). If there is only one line, then I don't use them. It looks better to me.

+2

Must use when there are more than 1 statement

+2

This is called intend poeted :

+2

I will use curly brackets for one-line blocks if I feel the code is "important", or complicated. Curly brackets and maybe a line of whitespace between the brackets and the line of code lets the code breathe and makes it more prominent. For less important code I will drop the brackets. Imo there is a difference between if (i == 0) return options.default; and if (i == 0) { return options.default; } When skimming and reading through code.

+2

Correct me if I'm wrong, but are curly brackets there so you can actually write C++ code in a signle line, no indentations? HonFu Now that's something Python can't do :)

+2

One word. Scope.

+2

For me I use curl braces when writing the main function which is obvious and the I use them when writing codes for control statements