0
int main() {
  int y = 8;
  int x = 4;
  for (int i = 0; i < 4; i++) {
    if (x == 4) {
      if (y == 7) {
        break;
      }
    }
    cout << "hey";
    y -= 1;
  }
}

The above code is code I wrote to test break in VS code. It's really weird because I got different behaviors in VS code and VS. In VS studio, break works as expected: the first run, y != 7 so we don't break and we print out "hey". Then, y == 7 and we break the loop, so we end up with just 1 "hey". When I copy pasted this code into VS code on the other hand, break literally does nothing. I have no idea why.

What could be the reason for this?

Tarick Welling
  • 3,119
  • 3
  • 19
  • 44
An Ignorant Wanderer
  • 1,322
  • 1
  • 10
  • 23
  • 1
    Maybe you need to flush the output. – drescherjm Sep 12 '19 at 03:17
  • 1
    Yah you should try with `std::cout << "hey" << std::endl` see https://stackoverflow.com/questions/14858262/stdcout-wont-print, or you might just need a `std::cin.get()` at the end to _see_ the output – Tas Sep 12 '19 at 03:25
  • Does the program keep running, or does it exit (and a normal exit, or a signal or something)? If it exits normally, it's guaranteed by the standard to flush output as part of calling certain destructors. – chris Sep 12 '19 at 03:41
  • Text output to the console that doesn't end in a newline does not necessarily get displayed. Try `std::cout << "hey\n";` and see if that changes things. (Yes, that's similar to what @Tas says, the key difference being that a flush, which is what `std::endl` does, isn't needed here). – Pete Becker Sep 12 '19 at 13:18

0 Answers0