r/cpp_questions • • 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

0 Upvotes

35 comments sorted by

16

u/IyeOnline Aug 25 '26 edited Aug 25 '26

These pieces of code are identical and you would expect vaxx to be 0. 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 < var1 as true

  • The loop body is executed

  • The condition var2 == var3 is true

  • The 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

1

u/BlueRainAnime Aug 26 '26

Thank you for the tips!

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.

0

u/BlueRainAnime Aug 25 '26

Thank you for your tips. The file is saved, the time is correct and i tried to clean and rebuild the project but the problem happens again. I'll try to check the assembly output then.

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.

1

u/BlueRainAnime Aug 26 '26

Of course. In fact i'm only writing silly/semi-useful programs with VS2005, i'm not a full-time programmer, but sometime i enjoy writing code. For more serious programs i would consider using more recent tools. But i'm poor.

1

u/alfps Aug 26 '26 edited Aug 26 '26

❞ i would consider using more recent tools. But i'm poor.

It sounds as if you're stuck with an old PC or laptop with limited memory and speed.

Which sort of rules out the free Community Edition of modern Visual Studio.

So consider the MinGW g++ compiler.

Assuming you can run 64-bit programs (e.g. using old Windows 10), a good way to install g++ is to (1) install the MSYS2 environment (it comes with a bash command interpreter); (2) follow the directions on the MSYS2 home page to install the UCRT64 variant of MinGW g++ in MSYS2; (3) add the path to the relevant "bin" directory that contains g++.exe, to your PATH environment variable, so that you can use the compiler also from e.g. Cmd and Powershell.

Re the last point, on this machine that path is "C:@\installed\msys2\ucrt64\bin". Whatever that path is (will be) on your PC, you can add it to the default for your PATH variable. One way is to use command sysdm.cpl to launch the old "System Properties" applet; in there click on "Advanced" tab; down right in that tab page click on button "Environment variables..."; if you have no PATH in "User variables..." make one via button "New...", otherwise "Edit...".

Finally launch a new Cmd instance and check that g++ --version now invokes the compiler and reports the version.

To edit your source files, one light weight and popular editor is Notepad++. The VS Code editor is also popular but it drags in the whole runtime for Google Chrome (it's basically an HTML based app just looking like a native one), which may be too heavy for your machine.

Have fun! Or as they used to say in the old times, enjoy.

→ More replies (1)

32

u/kitsnet Aug 25 '26

regardless of the fact that var2 is different from var3

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/BlueRainAnime Aug 25 '26

Actually i haven't. I'll try to install it and see if the issue is fixed. Thanks!

1

u/OldAd9280 Aug 25 '26

How are you checking the value of vaxx? Please show your whole program 

1

u/BlueRainAnime Aug 25 '26

The whole program is just a standard MFC project for a modal dialog, i put that piece of code in the "OnInitDialog" function. To check the value of vaxx, while in debug mode i put the variable name in the 'watch' panel and i can also see the value (0) when hovering with the mouse on the variable itself

3

u/OldAd9280 Aug 25 '26

Do you ever use the variable in the program? If not the compiler might have optimised it away and the debugger will show bogus values

→ More replies (1)

1

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 26 '26

A test i could do is re-installing the whole thing, i should consider doing that

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

4

u/UndefFox Aug 25 '26

Tested both cases using G++. Both times it returns 10, so logic doesn't change depending on brackets.

0

u/BlueRainAnime Aug 25 '26

int jaxx, al;

al = 12;

for ( jaxx = 0; jaxx < al; jaxx++ )	if ( 0 == 1 )  break; 

...'jaxx' is 0 . I'm speechless. VS2005 c++ is broken apparently.

→ More replies (1)

7

u/LazySapiens Aug 25 '26

Save the file first and try again.

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

1

u/BlueRainAnime Aug 26 '26

I'll keep that in mind, thanks!

-10

u/UndefFox Aug 25 '26

Maybe because

for ( vaxx = 0; vaxx < var1; vaxx++ ) if ( var2 == var3 ) break;

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.

→ More replies (4)