-2

Why is break statement executed but continue statement is not?

vector<int>val {2,3,4,-1,5,6,-1,56,234,-99,26,35,12,3,4};

for (auto name: val) {

    cout << name << endl;
    if (name == -1)
        continue;
    else if (name == -99)
        break;
}

Why does it not skip -1 (not execute continue statement) as indicated above in the code but at the same time it execute the break function. It displays following numbers to the console:

2
3
4
-1
5
6
-1
56
234
-99
user207421
  • 305,947
  • 44
  • 307
  • 483
Etchart
  • 39
  • 5
  • 4
    What makes you believe `continue` is not executed? What did you expect to happen differently? – Igor Tandetnik Aug 03 '19 at 04:12
  • I expected that there should be no '-1' displayed to the screen. – Etchart Aug 03 '19 at 04:13
  • 5
    How so? You print `name` before you check its value. Do you expect `continue` to somehow retroactively undo the effects of previous statement? – Igor Tandetnik Aug 03 '19 at 04:13
  • But then why break statement is executed even though I have printed name before – Etchart Aug 03 '19 at 04:14
  • 2
    `-99` does appear in the output, does it not? How come you are fine with that, but find the presence of `-1` surprising? – Igor Tandetnik Aug 03 '19 at 04:15
  • Look, when -99 was executed , the break statement also executed due to which no further number appeared (26,35,12,3,4) in this case.But when -1 was executed then why (unlike the case of -99) continue was not executed? – Etchart Aug 03 '19 at 04:18
  • 1
    It was. `continue` jumps to the beginning of the loop, and starts processing the next value. And indeed, the next value is happily printed. What again seems to be the problem? What exactly do you expect to happen after `-1` is printed? – Igor Tandetnik Aug 03 '19 at 04:19
  • put the cout after if- else – samini Aug 03 '19 at 04:19
  • 1
    I think the fact that the `continue` here doesn't do anything meaningful may be causing confusion – kmdreko Aug 03 '19 at 04:19
  • 1
    The code prints the value BEFORE checking it, so the `continue` has no observable effect. – Peter Aug 03 '19 at 04:23
  • @samini yes it works – Etchart Aug 03 '19 at 04:24
  • @Peter then why break statement has observable effect. – Etchart Aug 03 '19 at 04:25
  • @IgorTandetnik I expected that 2 3 4 5 6 56 234 -99 should have been displayed on the console with no -1. – Etchart Aug 03 '19 at 04:26
  • 1
    @Etchart The break is more obvious because it causes the loop to exit. The continue has an effect as well, it's just that causing the loop to advance doesn't show anything obvious because in this simple case, there's no difference between using a continue and letting the loop advance normally. Play around with continue is other examples to get a better feel for it. – Carcigenicate Aug 03 '19 at 04:27
  • 1
    Again, why do you expect `-99` to be printed, but `-1` to somehow magically not be? In both cases, you print the value first thing, before you check it and perform further processing. – Igor Tandetnik Aug 03 '19 at 04:28
  • @IgorTandetnik Sorry my bad! Yeah , I should not have expected -99 to be displayed ... but why then other numbers are not displayed (after -99) when the code does not executes. – Etchart Aug 03 '19 at 04:35
  • 1
    What do you mean, code doesn't execute? `break` breaks out of the loop, so no more elements are processed. `continue` starts the next iteration of the loop, so the next element is processed. Everything is working as it's supposed to. I fail to grasp the nature of the difficulty. – Igor Tandetnik Aug 03 '19 at 04:37
  • @IgorTandetnik I am a beginner so maybe I am misinterpreting stuff! Let me understand break and continue statement more and if I will have doubt, I'll ask here later!! – Etchart Aug 03 '19 at 04:42
  • @IgorTandetnik look, continue forces next iteration of the loop and yes it displays further numbers.I am fine with this.continue should have skipped '-1' ,isn't it? This is my doubt that why -1 was not skipped. Further, when name==-99, break statement hits and loop terminates which means that all number after -99 (inclusive) should not have been displayed. Then why -99 is displayed even when the break statement here means that -99 will not be displayed – Etchart Aug 03 '19 at 05:00
  • 1
    @Etchart The print statement is *before* the `continue` and `break` statements. So the number will be printed out first, and then the loop either skips to the next iteration or terminates. Code is executed in order from top to bottom. The print statement executed first, and then the if statements. `continue` or `break` won't undo what the print statement did. – eesiraed Aug 03 '19 at 05:21
  • 3
    You *first* print `-1`, and *then* execute `continue`. You *first* print `-99`, and *then* execute `break`. Neither `continue` nor `break` can travel back in time and undo the effect of statements already executed before them. – Igor Tandetnik Aug 03 '19 at 05:26
  • @IgorTandetnik Ok thanks! It all makes sense now. – Etchart Aug 03 '19 at 05:50

4 Answers4

3

Wrong placement of std::cout command, if all the conditions are faulty only then you have to display. If condition name == -1 is satisfied, the continue keyword instructs to go back immediately to the for-loop after incrementing the loop iterator, so cout command line will not be called in this case.

for (auto name : val) {     
    if (name == -1) 
        continue;
    else if (name == -99) 
        break;
    cout << name << endl;
}
seccpur
  • 4,996
  • 2
  • 13
  • 21
  • 1
    Not an answer to question. Question is "Why continue statement is not executed?". You should write this comment in a comment section, not as an answer. –  Aug 03 '19 at 05:37
  • You're right that the `continue` in the original code doesn't do anything, but you should probably give more of an explanation of why this change shows the problem. – Pete Becker Aug 03 '19 at 13:55
  • Didn't expect this question to get so much attention. Thought it was just OP's typos.I have edited slightly to elaborate my answer. – seccpur Aug 03 '19 at 14:34
  • @seccpur Thanks for the explanation! I think it makes sense now.However, at times there are some code I have written which work even when I put cout statement prior to `if else` statment, but i guess I should put it after the `if else` statment to reduce any chances of errors. – Etchart Aug 03 '19 at 17:55
0

coninue statement is not executed, because it's last statement in for loop and is meaningless, so optimizer removed it not to generate superflous jmp (or related) operation.

  • How come the `continue` is the last statement of `for` loop. Its `break` which is at the last and it is executed. Isn't it? – Etchart Aug 03 '19 at 05:53
  • @Etchart `continue` is the last statement of `statement-true` statement in `for` loop statement, but not necessarily of `for` loop sattement itself. While `init-statement` does not takes place after neither `statement-true` nor `statement-false`, `statement-true` can take place before `statement-false` and vice versa. Even likelihood attributes, while allows the compiler to optimize towards certain statement, can't ensure order of `statement-true` and `statement-false` statements. Nonetheless, `continue` statement was removed by compiler, together with the whole `statement-true` of first `if`. –  Aug 03 '19 at 07:09
-1

In your case, you should print the values only after they have been passed through the IF ELSE conditions. You're printing them before even checking if it is -1 or -99, so technically, it'll print everything in that vector, till -99 is found.

Tony_Stark
  • 62
  • 3
  • Why till -99?Why not after -99,the values are not displayed – Etchart Aug 03 '19 at 04:40
  • @Etchart Because `break` stops the loop. – eesiraed Aug 03 '19 at 04:53
  • @Etchart - you really do need to get a basic C++ book. See: [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?r=SearchResults&s=1|595.2486) – David C. Rankin Aug 03 '19 at 05:00
  • @AlexanderZhang yeah `break` does stop the loop but then -99 also shouldn't display. isn't it? – Etchart Aug 03 '19 at 05:02
  • 1
    @Etchart The break statement is after the print statement. C++ code executes in order, so the number is printed first, and then the loop terminates. I suggest you step through the code with a debugger to see how the program is executed. – eesiraed Aug 03 '19 at 05:18
  • Not an answer to question. Question is "Why continue statement is not executed?". You should write this comment in a comment section, not as an answer. –  Aug 03 '19 at 05:37
-1

First execution will happen step by step.

The for loop iterate one by one and compare each elements with given conditions.

Iteratively compare the name == -1 if its true the continue take the action else if name == -99 then break will take the action. Then you have to print the dissatisfied values.

So your code will work as below.

for (auto name : val) {
    if (name == -1)
        continue;
    else if (name == -99)
        break;
    cout << name << endl;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Every action you have attributed to the compiler here is performed at runtime, not by the compiler. The compiler isn't even present at runtime, let alone being in control of the execution of the program. – user207421 Aug 03 '19 at 05:27
  • It's not true that "First compiler will compare the \"name == -1\"". Compiler may check `else` condition first instead. –  Aug 03 '19 at 05:41
  • @ user207421 "Every action you have attributed to the compiler here is performed at runtime, not by the compiler." That's not true. Compiler optimizes the code and removes redundant statements. In order to do so, it has to perform those conditions first. Note that op asked "Why continue statement is not executed?", stating that `continue` statement is not executed, this it had to be removed by optimizier during compilation. –  Aug 03 '19 at 05:45
  • 1
    I tried to explain about the excution process."Why continue statement is not executed?".yes continue statement excution happening but the Wrong placement of std::cout command. so your getting output like that. so i tried to explain that . not about compiler. – Arun Pandian k Aug 03 '19 at 05:51