-2

I would like to know if there is an elegant way to create a GoTo LABEL structure with Python such as it is possible on Visual Basic. I have read that it kind of depends on case to case, but let's assume that I have a loop like the following (executable):

for i in range(0,10):
    print "LOOP", i, "- DO SOMETHING ALL TIMES"
    print "LOOP", i, "- DO SOMETHING SOME TIMES"
    print "LOOP", i, "- DO SOMETHING ELSE ALL TIMES"
    print ""

What I would like to know is if it's possible to skip the middle instruction, namely print "LOOP", i, "- DO SOMETHING SOME TIMES", with a construction like the following that (badly simulating the Visual Basic syntaxis) should allow the script to skip the instruction "do something some times" if i is equal to 3:

for i in range(0,10):
    print "LOOP", i, "- DO SOMETHING ALL TIMES"
    if i == 3: (GoTo SECONDPART)
    print "LOOP", i, "-DO SOMETHING SOME TIMES"
    SECONDPART:
    print "LOOP", i, "-DO SOMETHING ALL TIMES"

I know that I could write if i != 3: and indent the instruction "do something some times", but I'd really like to know if possible to have (or himitate) the GoTo structure of other languages. Thanks a lot, I hope it's clear (if not don't hesitate to ask!)

Matteo NNZ
  • 11,930
  • 12
  • 52
  • 89
  • The first script is executable as it's all written in Python, while the second is NOT cause contains parts of syntaxis from Visual Basic. – Matteo NNZ Jan 21 '14 at 12:16
  • 2
    This is a question being asked over and over again. And always, the answer is no, don't use goto even if available. – TwilightSun Jan 21 '14 at 12:18
  • No, Python has no goto nor an elegant way to fake it. – RemcoGerlich Jan 21 '14 at 12:21
  • Wow guys, I'm new to Stack Overflow but I'm just thinking, how can a person learn something if you answer such things as "don't use goto even if available" and vote down questions just for fun? Give an explanation at least, for what I think indenting hundred lines of code is less convenient than using a GoTo statement that verifies exactly the same condition and behaves exactly the same way, I really don't see the point of being so adverse when something is not possible or convenient (again, without at least explaining WHY to the beginner) :/ – Matteo NNZ Jan 21 '14 at 12:41
  • 1
    No self-respecting programming language (except maybe very low-level) should have a `goto`. If you think it is too much work to indent the lines use a better editor or think about putting that part of code in a new function. – Matthias Jan 21 '14 at 12:44
  • 1
    @MatteoNNZ The "case against goto" is well-known CS litterature : http://www.cs.utexas.edu/~EWD/transcriptions/EWD02xx/EWD215.html – bruno desthuilliers Jan 21 '14 at 13:02
  • Thanks @brunodesthuilliers, this is what I consider to be a constructive answer/comment with a complete explanation of the problem. – Matteo NNZ Jan 21 '14 at 13:23
  • @Matthias I have heard about this but never understood precisely why, since I'm a non-expert programmer and noticed that if I write "if this then this" or "if this goto that" was apparently the same thing. – Matteo NNZ Jan 21 '14 at 13:25
  • @MatteoNNZ : I learned the hard way what "goto hell" really means ;) – bruno desthuilliers Jan 21 '14 at 18:43

3 Answers3

1

Actually you might find that for a decent amount of situations, well placed if statements (without an else) can work well.

For example, with your example

for i in range(0,10):
    print "LOOP", i, "- DO SOMETHING ALL TIMES"
    if i != 3: 
       print "LOOP", i, "-DO SOMETHING SOME TIMES"
    print "LOOP", i, "-DO SOMETHING ALL TIMES"
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
rtpg
  • 2,419
  • 1
  • 18
  • 31
  • Note that the `<>` syntax has been deprecated for years, if not decades, and has finally disappeared in Python 3. – gerrit Jan 21 '14 at 12:27
  • Yeah I know in my example I could have done this, but I just wanted to know if there's any other work around than indenting tons of lines of code if some conditions are met. Thanks anyway :) – Matteo NNZ Jan 21 '14 at 12:28
  • 1
    @MatteoNNZ : yes: put this code into a function (and more probably into a set of functions) and just call the function. Goodbye "tons of lines of code", hello ReadableCode. – bruno desthuilliers Jan 21 '14 at 12:52
  • @gerrit I keep on forgetting which one is deprecated and which one isn't – rtpg Jan 21 '14 at 15:49
0

There is no goto in python. It even was easter egg in the past (that they will introduce goto to python), but it never happened and will never happen. goto construct is ugly itself, and most languages keep it just because they started with it (and they started with it, because everybody did so)

Filip Malczak
  • 3,124
  • 24
  • 44
0

You can with perverse manipulation of the runtime, but for what gain? I use goto in C (burn me on a stake) but the use case is severly limited.

In this case you would actually gain a extra line of code for no good reason.

Skurmedel
  • 21,515
  • 5
  • 53
  • 66