### Halit's blog

By Halit, history, 4 months ago,

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?

• +72

 » 4 months ago, # |   +6 Who told you not to?
•  » » 4 months ago, # ^ |   +4 I wondered because nobody using it
•  » » » 4 months ago, # ^ |   0 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, # ^ |   0 sorry for that. I should say mostly. Thank you for comment. So it isn't slow or something else?
•  » » » » » 4 months ago, # ^ |   0 I haven't tested it but I see no reason for that to be slower, if not faster.
•  » » » 4 months ago, # ^ |   0 i just happened to check Geothermal's submissions today in the recent Div 3. That's where i found it useful. :)
 » 4 months ago, # |   +24 goto isnt used because it leads to spaghetti code, but I don't think we care about that in CP lol
 » 4 months ago, # |   0 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, # |   0 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, # ^ |   0 I agree :)
 » 4 months ago, # | ← Rev. 2 →   +2 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.
•  » » 4 months ago, # ^ |   +3 Thanks!
 » 4 months ago, # |   +14 I am going to goto use it xD
 » 4 months ago, # |   +1 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, # ^ |   0 It's ok but not necessary, You can use goto. I think it is hard to read&write
 » 4 months ago, # |   +3 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, # ^ |   +3 you need a ';' after the label.
•  » » » 4 months ago, # ^ |   +3 oops, my bad. thanks
 » 4 months ago, # |   0 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.