Halit's blog

By Halit, history, 4 months ago, In English

Hi!

I learnt goto keyword in C++. But I couldn't see any code with using goto. So Why I shouldn't use goto? I want to use for this situation:

for(int i = 1;i <= n;i++){
   for(int j = 1;j <= n;j++){
       //some codes
       if(break all fors)
         goto q;
       //some codes
   }
}
q:

When I don't know goto keyword I am doing like this:

for(int i = 1;i <= n;i++){
   bool br = false;
   for(int j = 1;j <= n;j++){
       //some codes
       if(break all fors){
         br = true;
         break;
       }
       //some codes
   }
   if(br)
      break;
}

It's easy to see second code is longer.So why anyone doesn't use goto keyword?

 
 
 
 
  • Vote: I like it
  • +72
  • Vote: I do not like it

»
4 months ago, # |
  Vote: I like it +6 Vote: I do not like it

Who told you not to?

  • »
    »
    4 months ago, # ^ |
      Vote: I like it +4 Vote: I do not like it

    I wondered because nobody using it

    • »
      »
      »
      4 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Well I do, mostly for escaping multiple loops like you've shown. I think you shouldn't claim that nobody uses something unless you've checked every single users' coding style.

      • »
        »
        »
        »
        4 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        sorry for that. I should say mostly. Thank you for comment. So it isn't slow or something else?

        • »
          »
          »
          »
          »
          4 months ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          I haven't tested it but I see no reason for that to be slower, if not faster.

    • »
      »
      »
      4 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      i just happened to check Geothermal's submissions today in the recent Div 3. That's where i found it useful. :)

»
4 months ago, # |
  Vote: I like it +24 Vote: I do not like it

goto isnt used because it leads to spaghetti code, but I don't think we care about that in CP lol

»
4 months ago, # |
  Vote: I like it 0 Vote: I do not like it

It's just a matter of personal taste. I use goto to jump out multiple loops and probably only for this purpose.

»
4 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I think it is a very good thing. It is better than if(f == 1) break. But, I don't know it is fast or not ( in my opinion, it is not slow).

»
4 months ago, # |
Rev. 2   Vote: I like it +2 Vote: I do not like it

Here's a cool article on goto. It discusses its history and why it is now believed that it leads to bad code. It discusses the places where it does lead to spaghetti code, like when it replaces loops and function calls. And places where it doesn't, like the example you provided.

Article Link: https://geometrian.com/programming/tutorials/gotogood/index.php

»
4 months ago, # |
  Vote: I like it +14 Vote: I do not like it

I am going to goto use it xD

»
4 months ago, # |
  Vote: I like it +1 Vote: I do not like it

I do not like "flag programming" (as I call it) from sample 1, so when I need to break many loops, I restructure my code like as follows:

bool internal_loop(...)
{
   for (int j = 1; j <= n; j++) {
       // some code
       if (internal loop broken)
         return false;
   }
   return true;
}

  for (int i = 1; i <= n; i++) {
    if (!internal_loop(...))
      break;

    // some code
  }

Actually, I do the same in other cases too just to remove nested code into its own function so getting the code more maintainable. But who cares about maintainability doing CP, right? ;)

  • »
    »
    4 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    It's ok but not necessary, You can use goto. I think it is hard to read&write

»
4 months ago, # |
  Vote: I like it +3 Vote: I do not like it

Well, here's my story

I don't know why, but I don't like goto, but some time ago, after 3 years of doing cp I finally decided to use goto:

for(int i = 1; i <= n; i ++)
{
	for(int j = 1; j <= n; j ++)
	{
                //something here
		goto l1;
	}
	l1:
}

But, compilator: expected primary-expression before '}' token
And I promised myself not to use goto anymore -_-

»
4 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Actually it is pretty useful, I've been using it without problems so far. submission from recent contest. it's much cleaner than using flags to exit out of three nested loops for example.