Squiggly brackets for single statements ??

I don't really use brackets for a single statement under loops and if statements
But, just now I came across a problem that I had never encountered before.
I am wondering if it happens on just my laptop or there is a reason for it.

Here is the problem (by the way, I am using Visual Studio)

1
2
3
4
5
6
for (size_t i = 0; i < 3; ++i)
   if (true)
      std::cout << i << ' ';


return 0;


With a break point on the return statement, my screen should display

0 1 2

and should pause at the "return 0" statement before the program exits with a return value of 0
However, the loop makes a stop on every iteration. After displaying 0 and it waits until I press Continue, then it displays 1 and stops again. So I hit Continue again, and then it displays 2. If I put brackets like below it works just fine
1
2
3
4
5
6
7
for (size_t i = 0; i < 3; ++i)
{
   if (true)
      std::cout << i << ' ';
} // or instead of brackets, simple statement like std::cout << '\n' makes it work too

return 0;


Is it only happening to me ? or is there a reason? I never put brackets for single statments and have never had any problems with this.


Last edited on
IIRC visual breakpoints can be tricky for a few edge cases.
What you are seeing is entirely an issue with the VS debugger/ breakpoint handling and nothing at all to do with actual code. The simple thing may be to just do whatever you need to run the debugger for now and clean it up after if it bothers you. I mean we can waste time trying to figure it out, but its just the debugger being funky.

That said I always do add brackets on serious code because often find I need to go back to add a second statement or a debug print statement and forget to add brackets. its easier to just always put them in for me. I only leave them off on half page throw-away type programs, where I freely admit there isnt much I won't do in those as I usually just want to get it done rather than craft a thing of beauty.

Last edited on
Topic archived. No new replies allowed.