5

I want to skip some line of code by checking if condition in the same method. I used goto statement but it showed "cannot jump from this goto statement to the label". Is there any other way I can skip code? what I do is..

if(condition)
       goto skipped;
 //code to skip
 //code to skip

skipped:
 //code to execute
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Jashu
  • 1,061
  • 13
  • 19
  • 4
    Update your question with the real code you tried to use and show where and what the errors are. And explain why you can't use a normal `if/else` instead of `goto`. – rmaddy Feb 06 '16 at 05:04
  • The top answer for this question is pretty good. In general you shouldn't use `goto`. http://stackoverflow.com/questions/3517726/what-is-wrong-with-using-goto – EmilioPelaez Feb 06 '16 at 06:01
  • @EmilioPelaez in general, however, for command line tools and methods that literally would otherwise repeat code, goto is perfectly correct to use. Otherwise you are really repeating yourself. In Objective-C, a longish method that is of the kind that returns BOOL and has an outError out parameter, is a great use case, when steps within that method call other methods with the same pattern (NSFileManager, NSWorkspace etc...) – uchuugaka Jun 21 '17 at 06:09
  • Did you find the problem Jashu? I had no problems with goto in obj-c – Mars Jan 22 '18 at 09:31

2 Answers2

6

There are surely legitimate uses for goto, so I think this deserves a proper answer:
Some possible reasons why you might be failing:

  • Scope issues (Trying to jump to new functions, etc)
  • Jumping across declarations/destructors

Read more : http://en.cppreference.com/w/cpp/language/goto

Mars
  • 2,505
  • 17
  • 26
-3
if (!condition){
    //code to skip if condition == YES ...
}

// continue with code which executed regardless of condition

We don't use goto in object oriented programming. There's always a better way.

Jef
  • 4,728
  • 2
  • 25
  • 33
  • 2
    Don't use `condition == NO`. Use `!condition`. – rmaddy Feb 06 '16 at 05:34
  • Sure. I just didn't want to explain what ! means. You're right in terms of objC bools not being a first class boolean but I considered that kinda beyond scope. I do agree 100% and thanks, fixed – Jef Feb 06 '16 at 05:36
  • BTW - Using `condition != YES` is worse than using `condition == NO`. It really needs to be either `if (condition)` or `if (!condition)` as needed. – rmaddy Feb 06 '16 at 05:47
  • 2
    Some people like `condition == false` more than `!condition`. It really depends on wether you're working with people who have established a convention already. Both are extremely readable. In my opinion, this is one of those things that's like stepping over a $1 to pick up $0.10. – Oxcug Feb 06 '16 at 06:09
  • 1
    "We don't use goto in object oriented programming" - I think you meant in structured programming surely? We stand on the shoulders of [Dijkstra](http://homepages.cwi.nl/~storm/teaching/reader/Dijkstra68.pdf). – CRD Feb 06 '16 at 07:58
  • 2
    I used to be a `!condition` kind of guy. Then I spent several months working on other people's code. Now I always write `condition == NO` or `condition != NO`. – Avi Feb 06 '16 at 18:05
  • @rmaddy why does it really need to be `if (condition)` or `if (!condition)` ? Although common, the == or !== NO is for more clear than relying on condition value's naming convention. – uchuugaka Jun 21 '17 at 06:02
  • 4
    This is not even an answer to the question? How does this answer `how to use goto`. Also there are plenty of good reasons to use goto, for example breaking all the way out of nested loops. – Albert Renshaw Nov 12 '18 at 07:15