# goto - ever used it?

**URL:** <https://forum.arduino.cc/t/goto-ever-used-it/368902>\
**Category:** Programming\
**Created:** [February 29, 2016, 7:57am UTC](https://forum.arduino.cc/t/goto-ever-used-it/368902 "2016-02-29T07:57:11Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChrisTenone](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/christenone/32/219804_2.png) [@ChrisTenone](https://forum.arduino.cc/u/ChrisTenone)\
**Post date:** [February 29, 2016, 7:57am UTC](https://forum.arduino.cc/t/goto-ever-used-it/368902/1 "2016-02-29T07:57:11Z")

</div>

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:

```cpp
goto: skipOLED;

```

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

```cpp
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:

```cpp
    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?

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [February 29, 2016, 8:05am UTC](https://forum.arduino.cc/t/goto-ever-used-it/368902/2 "2016-02-29T08:05:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DrAzzy](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@DrAzzy](https://forum.arduino.cc/u/DrAzzy)\
**Post date:** [February 29, 2016, 8:08am UTC](https://forum.arduino.cc/t/goto-ever-used-it/368902/3 "2016-02-29T08:08:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ChrisTenone](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/christenone/32/219804_2.png) [@ChrisTenone](https://forum.arduino.cc/u/ChrisTenone)\
**Post date:** [February 29, 2016, 8:22am UTC](https://forum.arduino.cc/t/goto-ever-used-it/368902/4 "2016-02-29T08:22:12Z")

</div>

> 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!

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [February 29, 2016, 8:32am UTC](https://forum.arduino.cc/t/goto-ever-used-it/368902/5 "2016-02-29T08:32:50Z")

</div>

I have to goto bed 😴

---

<div class="post-metadata">

**Author:** ![MorganS](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/morgans/32/218718_2.png) [@MorganS](https://forum.arduino.cc/u/MorganS)\
**Post date:** [February 29, 2016, 8:34am UTC](https://forum.arduino.cc/t/goto-ever-used-it/368902/6 "2016-02-29T08:34:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ChrisTenone](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/christenone/32/219804_2.png) [@ChrisTenone](https://forum.arduino.cc/u/ChrisTenone)\
**Post date:** [February 29, 2016, 8:36am UTC](https://forum.arduino.cc/t/goto-ever-used-it/368902/7 "2016-02-29T08:36:50Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![ChrisTenone](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/christenone/32/219804_2.png) [@ChrisTenone](https://forum.arduino.cc/u/ChrisTenone)\
**Post date:** [February 29, 2016, 8:40am UTC](https://forum.arduino.cc/t/goto-ever-used-it/368902/8 "2016-02-29T08:40:13Z")

</div>

> LarryD:  
> I have to goto bed 😴

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

---

<div class="post-metadata">

**Author:** ![ChrisTenone](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/christenone/32/219804_2.png) [@ChrisTenone](https://forum.arduino.cc/u/ChrisTenone)\
**Post date:** [February 29, 2016, 8:45am UTC](https://forum.arduino.cc/t/goto-ever-used-it/368902/9 "2016-02-29T08:45:48Z")

</div>

> 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?

---

<div class="post-metadata">

**Author:** ![BulldogLowell](https://avatars.discourse-cdn.com/v4/letter/b/22d042/32.png) [@BulldogLowell](https://forum.arduino.cc/u/BulldogLowell)\
**Post date:** [February 29, 2016, 10:12am UTC](https://forum.arduino.cc/t/goto-ever-used-it/368902/10 "2016-02-29T10:12:22Z")

</div>

> 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](https://en.wikipedia.org/wiki/Omnipotence_paradox)...

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 29, 2016, 11:06am UTC](https://forum.arduino.cc/t/goto-ever-used-it/368902/11 "2016-02-29T11:06:39Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![jurs](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@jurs](https://forum.arduino.cc/u/jurs)\
**Post date:** [February 29, 2016, 12:16pm UTC](https://forum.arduino.cc/t/goto-ever-used-it/368902/12 "2016-02-29T12:16:05Z")

</div>

> 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

```arduino
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.

---

<div class="post-metadata">

**Author:** ![KeithRB](https://avatars.discourse-cdn.com/v4/letter/k/74df32/32.png) [@KeithRB](https://forum.arduino.cc/u/KeithRB)\
**Post date:** [February 29, 2016, 2:11pm UTC](https://forum.arduino.cc/t/goto-ever-used-it/368902/13 "2016-02-29T14:11:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KeithRB](https://avatars.discourse-cdn.com/v4/letter/k/74df32/32.png) [@KeithRB](https://forum.arduino.cc/u/KeithRB)\
**Post date:** [February 29, 2016, 2:14pm UTC](https://forum.arduino.cc/t/goto-ever-used-it/368902/14 "2016-02-29T14:14:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JimboZA](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jimboza/32/215171_2.png) [@JimboZA](https://forum.arduino.cc/u/JimboZA)\
**Post date:** [February 29, 2016, 2:49pm UTC](https://forum.arduino.cc/t/goto-ever-used-it/368902/15 "2016-02-29T14:49:40Z")

</div>

How's _this_ for irony....

 ![goto.JPG](https://europe1.discourse-cdn.com/arduino/original/4X/8/6/c/86cea3bc3b0d805254ab15e0daa11188216e0d1d.jpeg)

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [February 29, 2016, 3:02pm UTC](https://forum.arduino.cc/t/goto-ever-used-it/368902/16 "2016-02-29T15:02:43Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [February 29, 2016, 4:20pm UTC](https://forum.arduino.cc/t/goto-ever-used-it/368902/17 "2016-02-29T16:20:43Z")

</div>

Good morning.

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

---

<div class="post-metadata">

**Author:** ![BigBobby](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@BigBobby](https://forum.arduino.cc/u/BigBobby)\
**Post date:** [February 29, 2016, 9:05pm UTC](https://forum.arduino.cc/t/goto-ever-used-it/368902/18 "2016-02-29T21:05:37Z")

</div>

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:

```cpp
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.

---

<div class="post-metadata">

**Author:** ![el\_supremo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/el_supremo/32/214787_2.png) [@el\_supremo](https://forum.arduino.cc/u/el_supremo)\
**Post date:** [February 29, 2016, 9:28pm UTC](https://forum.arduino.cc/t/goto-ever-used-it/368902/19 "2016-02-29T21:28:30Z")

</div>

```arduino
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

```arduino
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

---

<div class="post-metadata">

**Author:** ![zoomkat](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/zoomkat/32/1190177_2.png) [@zoomkat](https://forum.arduino.cc/u/zoomkat)\
**Post date:** [March 1, 2016, 12:09am UTC](https://forum.arduino.cc/t/goto-ever-used-it/368902/20 "2016-03-01T00:09:59Z")

</div>

> 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.

[Next page](https://forum.arduino.cc/t/goto-ever-used-it/368902.md?page=2)
