Do goto blocks execute even if there is no goto?

Like this:

1
2
3
4
5
6
7
8
for (...) {
    if (...) {
        //do things
        goto h;
    }
}
h:
//do more things 


Is the h block going to execute even if the goto in the upper section doesn't?

-note: I have heard that goto is 'bad form', but this is really a perfect use for it, and unless someone can present me with substantive proof that it has some secret memory leak or something terrible like that, then I really don't care.
Last edited on
Substantive proof:

You can use a break.

break;

Yes.
h: will still execute.
-note: I have heard that goto is 'bad form', but this is really a perfect use for it, and unless someone can present me with substantive proof that it has some secret memory leak or something terrible like that, then I really don't care.


"I don't know what this does, but I'm going to do it!" You'll get far with that attitude.

The code as presented is precisely equivalent to:

1
2
3
4
5
6
7
for (...) {
    if (...) {
        //do things
        break;
    }
}
//do more things 
> Is the h block going to execute even if the goto in the upper section doesn't?

Yes. h: is just a label.


> it has some secret memory leak or something terrible like that

No, it does not have anything terrible like a secret memory leak


> I have heard that goto is 'bad form'

Yes, it is generally frowned upon if there are more elegant ways to get the same effect.

In this particular case, there probably is a cleaner way of writing the code.
(Need more context to say what that could be).
Last edited on
Thanks all!

Allthogh, there seems to be controversy about what I'm trying to accomplish:

1
2
3
4
5
6
7
8
for (...) {
    if (...) {
        //bla
        break;
    }
    //break will take me here instead of completely out of the for loop
}
//i need to get here 


Hence: goto.

And thanks for the answer.
Last edited on
@cire

I do actually know what goto does, I've heard of it, and I know it's frowned upon.

Allthough if what you're saying about break is true, I would be happy to do that (I thought break only exits out of one loop at a time).

And thanks again.
Last edited on
if is not a loop. break takes you out of the innermost loop. In your case, the for is in the innermost loop so it will take you out of it.
Break does not work for ifs, only loops and switches. http://en.cppreference.com/w/cpp/language/break

In your case you have an if inside a for loop. The break statement exits the most recently entered loop, which in this case means it breaks out of the for loop (in the process also exiting the if statement, but this is irrelevant because ifs don't affect break).
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Example program
#include <iostream>
#include <string>
using namespace std;

int main()
{
  for (int i = 0; i < 10; i++) {
      if (i == 4) {
        break;  
      }
      cout << i;   
  }
  cout << "Hello!";
  
  return 0;
}



Copy and paste this code into http://cpp.sh/
Last edited on
As a side note, one of my professors who is a C++ guru (idk if I should really say guru but he has worked in industry) said that you should also evade break and continue statements because they act like gotos. Is that really true? Should breaks and continues be avoided?
> Should breaks and continues be avoided?

break, continue and goto are are three different forms of jump statements.
They are birds of a feather; avoid them if more elegant alternatives are available.

1
2
3
4
5
6
7
8
9
10
11
    while( conditon_one )
    {
// start_of_loop:
            // ...
            if( condition_two ) continue ; // goto start_of_loop ;
            // ...
            if( condition_three ) break ; // goto exit_from_loop ;
            // ...
    }
// exit_from_loop:
    // ... 


1
2
3
4
5
6
7
8
9
10
11
    while( conditon_one )
    {
start_of_loop:
            // ...
            if( condition_two ) goto start_of_loop ; // continue
            // ...
            if( condition_three ) goto exit_from_loop ; // break
            // ...
    }
exit_from_loop:
    // ... 
Topic archived. No new replies allowed.