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
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.