How do I make a line of code that would go back to a part of a program without making it a loop?
For example

Code I don't want repeated

some line for later to come back (line 2)
$$$code I want to repeat$$$
jump back to line 2

What language is this being coded in?

Do you consider While (c, c++, others) to be a loop? Some consider loops to be the loop keyword (natch) or the word for. Tell us what language this is in. Also share why do this since there are some that are done for both good and bad reasons.

What language is this being coded in?

Tagged as C++.

How do I make a line of code that would go back to a part of a program without making it a loop?

C++ has the goto statement. I know it's blasphemy to many to use goto, but surely if it was always a bad thing it wouldn't still be in the language?

Code I don't want repeated
some line for later to come back (line 2)
$$$code I want to repeat$$$
jump back to line 2

Isn't the above a loop (see equivalent below)

Code I don't want repeated
while(true)
{
    some line for later to come back (line 2) // not 100% sure what this means
    $$$code I want to repeat$$$
}

Or maybe it's this...

Code I don't want repeated
someline:
    some line for later to come back (line 2)
    while(true)
    {
        $$$code I want to repeat$$$
    }

/* code */
goto someline;

You figure out what you need to accomplish, create a specification that corresponds to that goal, incorporating any restraints, priorities, etc. into that specification. Then you code. In my view, whether you use no loop, a for-loop, a while-loop, goto statements with labels, an endless loop with continue and break to get out of that loop, etc., is largely unimportant. I can convert one loop type to pretty much any other loop type without much difficulty.

goto works just fine, but it gives you an awful lot of rope to hang yourself with compared to sticking the code you want to repeat inside of a loop or a function.

So to RProffitt's point, I imagine the question is not how, but rather what and perhaps why. Nail those down and the how will become obvious.

@assertnull. When I replied I didn't find the tag so I asked. I wonder why it didn't show when I posted.

PS. Added with edit. The forum seems to have a new layout. Maybe someone moved the cheese.

Maybe someone moved the cheese.

Is the cheese the C++ tag in this metaphor? The layout changed, so the C++ tag wasn't in the normal position so you missed it? I see it in two spots (bottom of the thread and bottom of the initial post). The layout changed? Looks the same to me. How unobservant am I? Big change or small change? It does seem a little different, but for the life of me I can't specify what's different. I was always terrible at the "spot the difference" game.

Above post didn't come out as intended, so disregard. Having editing problems too. Anyway, cool phrase. Thought you'd coined it, but apparently it comes from a book.

My two cents - a goto should only be used as an escape. In some rare cases when you have code that has several levels of nesting and whenever an error occurs you want to just bail out, (I think) it is acceptable to use a goto to drop out to the outermost loop end (or module end, etc.). There are cases where this is much cleaner (clearer) than coding multiple flags and tests. I also believe that anyone who codes a goto in the "up" direction should be drawn and quartered.

commented: Severly flogged and salted! +15
commented: http://xkcd.com/292/ +14

Hopefully the OP will come back. I threw goto out there because I can't think of much else that would do what the OP is asking for.

Goto has been quite useful for me programming embedded software where RAM is extremely low and for optimizing code for speed. In addition, for state machines, I think they actually (sometimes) improve readability and reduce errors. For the most part though, the pitfalls that come with it are considerable, so if there's not some advantage outweighing those pitfalls, don't do it. Debugging goto bugs isn't very fun.

Never had the need to use a goto (progrmming mainly in C#), but occasionally I used a break or return to step out of a loop.

This is what happens when you turn to the dark side (this is an actual piece of production code as delivered to us by the vendor of a real time critical system back in 1983).

      SUBROUTINE READALL(UNIT,BUFF,*,*,IFIRST)
C     THIS SUBROUTINE IS USED FOR READING COMPRESSED OR UNCOMPRESSED
C     DATA INPUT FILES FROM LFC 'UNIT' INTO 80 BYTE ARRAYY BUFF.
C     END RETURNS TO FIRST RETURN, ERROR RTETURNS TO SECOND
C     IFIRST IS LOGICAL WHICH CALLER PROVIDES TRUE ON
C     FIRST READ OFF OF THIS ALLOCATION OF THIS LFC, ELSE FALSE
      LOGICAL IFIRST
      INTEGER*1 BUFF(80)
      INTEGER*4 UNIT
      INTEGER*1 BYTE(120,2)

      INTEGER*1 IN(2)/2*0/,ISP(2)/2*0/,ITX(2)/2*0/,NB(2)/2*0/
      INTEGER*1 NBT(2)/2*1/
      INTEGER*1 KCR/ZBF/,KLR/Z9F/
      INTEGER*1 EOR/ZFF/
      INTEGER*2 NSEQ(2)/2*0/,ITY(6),ISEQ(60,2),ICKS(60,2)
      INTEGER*4 LFC(2)/2*0/
      INTEGER*4 K,N,IERR,IN0,ISP0,ITX0,NB0,IS,I,ISUM,J

      EQUIVALENCE (BYTE(3,1),ICKS(1,1)),(BYTE(5,1),ISEQ(1,1))

      IF(.NOT.IFIRST) GO TO 21
      DO 19 K=1,2
      IN(K) = 0
      ISP(K) = 0
      ITX(K) = 0
      NB(K) = 0
      NBT(K) = 1
      NSEQ(K) = 0
      LFC(K) = 0
19    CONTINUE
21    CONTINUE
      DO 101 N=1,2
      IF (UNIT.EQ.LFC(N)) GO TO 103
      IF (LFC(N).EQ.0) GO TO 102
101   CONTINUE
      GO TO 94
102   LFC(N) = UNIT
      CALL W:PDEV(UNIT,ITY)
      NBT(N) = 1
      IF (ITY(3).GE.4.AND.ITY(3).LE.6) NBT(N)=2
103   IERR = 0
      IN0 = IN(N)
      ISP0 = ISP(N)
      ITX0 = ITX(N)
      NB0 = NB(N)
1     IF (IN0.NE.0) GO TO 8
2     CALL BUFFERIN(UNIT,0,BYTE(1,N),30)
      CALL M:WAIT(UNIT)
      CALL STATUS(UNIT,IS,NB0)
      IF (IS-3) 3,80,90
3     IF (BYTE(1,N).EQ.KCR.OR.BYTE(1,N).EQ.KLR) GO TO 6
      NB0 = NB0*NBT(N)
      DO 4 I=1,NB0
      IF (BYTE(I,N).EQ.10.OR.BYTE(I,N).EQ.13) BYTE(I,N) = 1R
4     BUFF(I) = BYTE(I,N)
      IF (NB0.GE.80) GO TO 20
      I = I+1
      DO 5 I=I,80
5     BUFF(I) = 1R
      GO TO 20
6     NB0 = BYTE(2,N)
      IF (NB0.GT.114) GO TO 91
      ISUM = 0
      DO 7 I=1,NB0
7     ISUM = ISUM+BYTE(I+6,N)
      IF (ISUM.NE.ICKS(1,N)) GO TO 92
      IF (ISEQ(1,N).NE.NSEQ(N)) GO TO 93
      NSEQ(N) = NSEQ(N)+1
      IN0 = 7
8     DO 16 I=1,80
      IF (ISP0.NE.0) GO TO 9
      IF (ITX0.NE.0) GO TO 12
      IF (BYTE(IN0,N).EQ.EOR) GO TO 9
      ISP0 = BYTE(IN0,N)
      IF (ISP0.GT.81-I) GO TO 91
      IN0 = IN0+1
      GO TO 10
9     ISP0 = ISP0-1
10    IF (ISP0.EQ.0) GO TO 11
      BUFF(I) = 1R
      GO TO 16
11    IF (BYTE(IN0,N).EQ.EOR) GO TO 9
      ITX0 = BYTE(IN0,N)
      IF (ITX0.GT.81-I) GO TO 91
      IN0 = IN0+1
12    BUFF(I) = BYTE(IN0,N)
      IF (BUFF(I).EQ.10.OR.BUFF(I).EQ.13) BUFF(I) = 1R
      ITX0 = ITX0-1
13    IN0 = IN0+1
      IF (IN0.LE.NB0+6) GO TO 16
      CALL BUFFERIN(UNIT,0,BYTE(1,N),30)
      CALL M:WAIT(UNIT)
      CALL STATUS(UNIT,IS,NB0)
      IF (IS-3) 14,80,90
14    IF (BYTE(1,N).NE.KCR.AND.BYTE(1,N).NE.KLR) GO TO 91
      NB0 = BYTE(2,N)
      IF (NB0.GT.114) GO TO 91
      ISUM = 0
      DO 15 J=1,NB0
15    ISUM = ISUM+BYTE(J+6,N)
      IF (ISUM.NE.ICKS(1,N)) GO TO 92
      IF (ISEQ(1,N).NE.NSEQ(N)) GO TO 93
      NSEQ(N) = NSEQ(N)+1
      IN0 = 7
16    CONTINUE
      IF (BYTE(IN0,N).NE.EOR) GO TO 91
      ISP0 = 0
      IF (ITX0.NE.0) GO TO 91
      IN0 = IN0+1
      IF (IN0.GT.NB0+6) IN0 = 0
      IF (BYTE(1,N).EQ.KLR.AND.IN0.EQ.0) NSEQ(N) = 0
20    IN(N) = IN0
      ISP(N) = ISP0
      ITX(N) = ITX0
      NB(N) = NB0
      RETURN
80    NSEQ(N) = 0
      IN(N) = ISP(N) = ITX(N) = 0
      RETURN 1
90    IERR = 1
91    IERR = IERR+1
92    IERR = IERR+1
93    IERR = IERR+1
94    IERR = IERR+1
      IN(N) = ISP(N) = ITX(N) = 0
      BUFF(1) = IERR
      RETURN 2
      END

When I first saw this I immediately unraveled and rewrote it using proper constructs.

This is what happens when you turn to the dark side

That's definitely the dark side.

Yeah! Spaghetti!!!

'Goto' exists in C++. Place a label where you want to loop to, and then "goto mylabel;" when you want to branch to it:

MyLabel:
/ Some code /
goto MyLabel;

is perfectly legitimate. Now, to head off the howls of anguish, no, the 'goto' statement is not inherently evil. The rampant abuse of it is inherently evil. When the proper thing to do is employ a control construct, then that should be the option of choice. But the goto statement persists to this day precisely because there are occasions where it is the correct control construct. Granted, they are very few, but there is no reason at all to fitzcarraldo a baroque while() - based construct to emulate a goto if all you require is the goto.

YrthWyndAndFyre you are right, but goto should only be used by compiler designers, not by us mere mortals.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.