goto - ever used it?

I almost used goto in one of my clocks tonight. I needed to turn the display off while setting (to comply with NIST Special Publication 960-14 (WWVB Radio Controlled Clocks), and to make the code run faster, thus causing less interference with the radio software. I wanted to just turn the time display off while setting. So I tried just adding:

goto: skipOLED;

where skipOLED: was used in the sketch at another location. I got an error that said:

label 'skipOLED' used but not defined
'skipOLED' was not declared in this scope

I looked at my copy of K&R, and decided I didn't want to learn about it, so I opted to use a bit more code and add a couple lines:

    case=0:
      break;

which accomplished the same thing.

I'll show all my code and my schematic if you want to see them, but my actual question is more philosophy:

Have you ever used goto in a sketch?
If so, why?

Never. Because no matter how complicated the code I've written has yet become, I haven't ever reached a point where I needed to use it. Also, I hated it so much when it was all there was (which was a long time before there were "sketches"). That feeling never wore off.

I have never used goto (did you try to goto a label in another function? You can't do that in C)

I hope I'll never have to. It is an abomination.

I try to avoid switch-case (I use it occasionally, but I don't like it - I have a lousy track record w/regards to not writing bugs when I use it) in favor of if/else too.

aarg:
...I hated it so much ...

I hear ya aarg! My first experiences with a real computer were with BASIC through a paper-tape terminal to an IBM mainframe. it was goto, goto, goto. I flubbed so many programs that way! Finally, in Organic Chemistry class, we had to calculate electron density with a computer (well, what we called a computer in 1972.) The FORTRAN example I used didn't use GO TO, and I totally dug it!

But ... every now and then, especially like tonight, where I didn't see a clear path, or during my darkest assembly days (M68000, PPC, G4, G5) I was tempted and succumbed I thought about goto. I learned FORTH for functional atonement, but that's gone now. I'm hoping for purity with Arduino and C. I am very glad that you do not use goto!

Is there anyone here who has found it necessary to use goto? -- I NEED TO KNOW!

I have to goto bed :sleeping:

It might serve a use if you need to break out of a nested loop. Break will leave one loop, but it won't leave two. You have to set a flag like "FoundIt" and then test for that in the outer loop to call a break out of that loop. A goto could simplify that and save on testing FoundIt on every iteration of the outer loop.

It's still not a good idea. The FoundIt flag is much easier to trace.

DrAzzy:
I have never used goto (did you try to goto a label in another function? You can't do that in C)

I hope I'll never have to. It is an abomination.

I try to avoid switch-case (I use it occasionally, but I don't like it - I have a lousy track record w/regards to not writing bugs when I use it) in favor of if/else too.

Totally agree on the dis-likeability of goto! I HATE it. I have written SO much spaghetti (vermicelli actually) code that I feel culturally Italian, but it's never lasted. As soon as I needed to do a major redo, it's meant a complete re-write.
I need to look up the syntax of switch/case every time, so ya, it's not my favorite either. BUT, when I find myself testing the same variable (If (var=1;) if (var=2); etc.) multiple times, then switch/case makes total sense.

LarryD:
I have to goto bed :sleeping:

I'll be there soon, but I'm west-coast, so it's millions of millis earlier here.
Soon, soon ...

MorganS:
It might serve a use if you need to break out of a nested loop. Break will leave one loop, but it won't leave two. You have to set a flag like "FoundIt" and then test for that in the outer loop to call a break out of that loop. A goto could simplify that and save on testing FoundIt on every iteration of the outer loop.

It's still not a good idea. The FoundIt flag is much easier to trace.

In theory using goto could save some bytes, which in theory that could make or break a program, right? But it's ugly and makes fixing things in the future more difficult. Especially if other people will be editing my code! I'm looking for the case where GOTO is the obvious (and possibly only) option.

Does that exist?

ChrisTenone:
I'm looking for the case where GOTO is the obvious (and possibly only) option.

Does that exist?

It seems to me that the problem with your question is that in programming there are usually many answers to even the most trivial of problems. No sooner does someone say "I used a this method here" than someone will show you how to program it another way. This site is the embodiment of that idea.

Is your question really "I'm stuck here and I cannot get out without using goto?" maybe someone here will have one of the potentially many possible ways to program it differently. If your question is "Can anyone conceive of a situation where goto is my only alternative?" then you may want to consider that a sort-of paradox.

Goto seems to be the infinite lever... is there an immovable object? The omnipotence paradox...

In theory using goto could save some bytes

You'd need to post an example to convince me. All that goto accomplishes is bypassing some code. The same thing can be accomplished in other, more readable/maintainable ways.

which in theory that could make or break a program, right?

I suspect that if you are that close to the limit, goto is probably the least desirable solution. Since I'm not convinced that goto saves memory, I'm less convinced that it will be the only solution that results in a program that fits vs one that doesn't fit.

ChrisTenone:
Have you ever used goto in a sketch?
If so, why?

For me the only reason to use goto in an Arduino sketch is to leave a set of nested for-loops from one of the inner loops before the normal end conditions are reached

f

for (int i=0;i<100;i++)
{
  for (int j=0;j<100;j++)
  {
    for (int k=0;k<100;k++)
    {
      if (someEndCondition) goto end;

    }
  }
}
end:

This creates some more obvious programming logic and a little bit less typing than without using goto.

In all other cases, goto is not of any use in structured C/C++ programming.

I have used it to short circuit a function and go directly to cleanup when an error occurs when controlling GPIB instruments.

There is nothing evil about goto - it can be just as hard to find the end of a set of nested braces. As long as you document it and the code intent is clear, no problem. I usually put in an 'ACCCCK! a goto!' comment and indicate where it goes too. I will say it is better to go "down" the code rather than "up" the code.

How's this for irony....

it can be just as hard to find the end of a set of nested braces.

Even the Arduino IDE lets you do that easily.

Good morning.

I have used goto a few times in debugging to jump around some code.
.

Times that I use goto:

  1. When I'm writing a bootloader or other code that needs to be tiny, and while goto is gross, it's still less gross than assembly.

  2. When I'm writing a state machine that I want to run until its final state, not just until it's advanced one state. Something like this:

STATE_TYPE State_Machine_Run(STATE_TYPE state)
{
  switch(state)
  {
    case STATE_1:
_state_1:
      if(var1)
      {
        state = STATE_2;
        goto _state_2;    // I could solve with a fallthrough, but it is that any better?
      }
      break;

    case STATE_2:
_state_2:
      if(var2)
      {
        if(var3)
        {
          state = STATE_3A;
          goto _state_3a;
        }
        else
        {
          state = STATE_3B;
          goto _state_3b;
        }
      }
      break;

    case STATE_3A:
_state_3a:
      if(!var1)
      {
        state = STATE_1;
        goto _state_1;
      }
      break;

    case STATE_3B:
_state_3b:
      if(var4)
      {
        state = STATE_4;
        goto _state_4;
      }
      break;

    case STATE_4:
_state_4:
      if(!var1)
      {
        state = STATE_1;
        goto _state_1;
      }
      break;

    case NUM_STATES:
    default:
      break;

  }

  return state;
}

Yeah, there are other ways around it. For example I could track state_previous and then put a while loop around the switch that runs until state == state_previous, running through the full switch for every case. With that solution I'd always run the switch one time unnecessarily so that I could see the state not change, unless I used returns in the case statements which are gross for other reasons. I'm open to better ways to make a state machine run to its final state.

goto: skipOLED;

That's not a goto statement. It's a label whose name is 'goto' followed by a statement which doesn't make sense.

This

goto skipOLED;

might work.

Have you ever used goto in a sketch?

No, but I used it a lot back in the "good old days" of Fortran and I don't mean the wimpy Fortran-77 etc. The REAL Fortran 66.
Then I went to university - and everything changed.

Pete

Have you ever used goto in a sketch?
If so, why?

I've used goto in a simple jump situation where developing logic to do the same thing appeared cumbersome and unneeded.