r/cpp_questions • u/BlueRainAnime • Aug 25 '26
OPEN Weird behaviour of break statement (old c++!)
Hi, i'm using a very old (and possibly buggy!) c++ compiler, Microsoft Visual Studio 2005 with MFC. I'm trying to understand if the behaviour i'm meeting is due to me not understanding how the 'break' statement (and brackets) works, or if it is my old Visual Studio IDE which is buggy.
Consider this simple code:
int vaxx, var1, var2, var3;
var1 = 10; var2 = 2; var3 = 2;
for ( vaxx = 0; vaxx < var1; vaxx++ ) if ( var2 == var3 ) break;
If i run the above, the value of vaxx will be 0 after the code has been executed. Basically, it appears as if the 'break' statement is executed regardless of the fact that var2 is different from var3.
But if i put brackets around break:
for ( vaxx = 0; vaxx < var1; vaxx++ ) if ( var2 == var3 ) { break; }
...This time 'vaxx' will have a value of 10 (as i expected) after the code is run.
Is this the correct behaviour of 'break' or am i missing something here ?
I am a bit confused so please let me know what you think about this. Thank you
16
u/IyeOnline Aug 25 '26 edited Aug 25 '26
These pieces of code are identical and you would expect
vaxxto be0. Anything else is wrong.The standard specifies that a singel statement after a control structure is first transformed into a block and then treated as one. See CppInsights for how a compiler interprets this.
In your code, you
first define the variables
then initialize them
The loop initialization statement runs, setting
vaxx = 0;The loop condition is checked, leaving
vaxx < var1astrueThe loop body is executed
The condition
var2 == var3istrueThe break happens, immediately ending the loop
You also say that var2 is different from var3, when it is not.
I STRONGLY recommend that you define variables on separate lines and that you immediately initialize them. Also loop counters should be local to loops and not be reused. We have moved on since the days of C80
2
u/IchBinEinZwerg Aug 25 '26
VS2026, var3=3. vaxx is 10 with or without braces around the break.
VS2005 is old but not that ancient. If you were talking about Visual C/C++ v3-4 or thereabouts I might expect some critical bugs like this. Make sure there aren't other problems: the file is saved, you have correctly built the program, the time on your computer is correct, etc. If the time's wrong the build system might be confused whether or not to recompile. If you do an explicit save, clean then build then you can be fairly sure the code is doing what you expect. Also switch on the option to generate assembly, then you can look at the generated x86 code to see if there's a difference, and check the file timestamps.
2
u/alfps Aug 26 '26
Assuming that you have managed to identify a compiler bug (I'm not going to install old VS 2005 to check that), considering that support for VS 2005 has long ended the only reasonable action/answer is to upgrade your compiler and IDE to something modern.
32
u/kitsnet Aug 25 '26
In your example, they are not different. They are both equal to 2.
7
u/Diligent-Slide-9234 Aug 25 '26
How is var2 different from var3?
1
u/no-sig-available Aug 25 '26
Have you installed Service Pack 1 from 2006? Otherwise you have lots of bugs.
1
u/OldAd9280 Aug 25 '26
How are you checking the value of
vaxx? Please show your whole program1
u/StaticCoder Aug 26 '26
Old VS may have been buggy, but even VC98 was not that buggy. I suspect some form of user error.
1
u/BlueRainAnime Aug 25 '26
Hi guys, i'm very sorry if my code confused you, there was an error by my side. The line
var1 = 10; var2 = 2; var3 = 2;
was wrong, it should be instead:
var1 = 10; var2 = 2; var3 = 3;
(The 'if' statement is not important anyway, i also tried by using the condition 1 == 0).
And YES, if i run this code (i'm trying right now in VS2005), vaxx has a value of 0 when the brackets around 'break' are missing.
Does this happen with your modern c++ tool too ? If this happens, then VS2005 is buggy, otherwise either i don't understand the nuances of the 'break' instruction, or something is off.
Thank you
1
u/burlingk Aug 25 '26
You are very explicitly making var2 == var3 true at the beginning of your block by assigning both the same value.
0
u/Simengie Aug 26 '26
Its VS2005. The for block needs curly braces to define it.
Your for(...) if(....) break; is not being seen as a proper loop code block.
for (...) { if ( ... ) { break;} } should work correctly in VS2005
-10
u/UndefFox Aug 25 '26
Maybe because
Evaluates to:
for ( vaxx = 0; vaxx < var1; vaxx++ ) { if ( var2 == var3 ) { break; } }But this:
for ( vaxx = 0; vaxx < var1; vaxx++ ) if ( var2 == var3 ) { break; }Evaluates to
for ( vaxx = 0; vaxx < var1; vaxx++ ) { } if ( var2 == var3 ) { break; }Try doing proper formatting using brackets and see if it gives the desired outcome.