Use of goto statement

I am trying to learn use of goto statement. Program will not compile. missing declaration? Help would be appreciated.

define fastStop

void setup() { pinMode(13, OUTPUT); pinMode (11,OUTPUT); }

void loop() { digitalWrite(13, HIGH); delay(5000); digitalWrite(13, LOW); delay(1000);

goto fastStop; //Compile stops at this point expected identifier before token

digitalWrite (13, HIGH); fastStop: { digitalWrite(11, HIGH); delay(5000); digitalWrite(11, LOW); delay(1000); } }

Goto is deprecated and doesn't allow logical program flow. It may be removed from future versions of c++ and should be avoided.

All you are doing is skipping a digitalWrite, just delete that line.

I think the error in this case is, fastStop is defined as nothing up the top of the sketch, fastStop is replaced with nothing on compile. So your line is actually:

goto ;

pYro_65:
I think the error in this case is, fastStop is defined as nothing up the top of the sketch, fastStop is replaced with nothing on compile.
So your line is actually:

goto ;

… which is a great reason not to use #defines! Wow, two birds with one stone.

n46130: I am trying to learn use of goto statement. Program will not compile. missing declaration? Help would be appreciated.

I strongly advise you not to learn how to use the goto statement.

Except in very very rare cases, it simply isn't needed.

Unfortunately this topic tends to generate a lot of heat, and no doubt my post will be followed up with a number of others explaining why I am an idiot.

Let me just say that in writing C (and C++) code for 20-odd years now, I have never needed to use goto. Occasionally I note that other advanced authors have used it in highly technical situations (like saving stack space when doing tail recursion ... if you don't understand what that means, you don't need to use goto).

Save yourself a lot of hassle: learn to use if, while, do, for, switch and function calls. Not goto.

[quote author=Nick Gammon link=topic=107886.msg810173#msg810173 date=1338357763] Let me just say that in writing C (and C++) code for 20-odd years now, I have never needed to use goto. Occasionally I note that other advanced authors have used it in highly technical situations (like saving stack space when doing tail recursion ... if you don't understand what that means, you don't need to use goto).

Save yourself a lot of hassle: learn to use if, while, do, for, switch and function calls. Not goto. [/quote]

I once got into an online discussion regarding goto, and someone showed a very interesting example of using goto in the context of a non-switch-case state machine; it was actually fairly elegant and interesting. There was also the common example of using goto to break out of deeply nested if-then constructs (though I tend to wonder if code refactoring might not be a better solution; but, at the same time, I can see why if you had a whole mess of if-then logic, where refactoring might make things worse, in that the code might already be well debugged - and refactoring could actually introduce more bugs or logic errors that might not be self-evident until actually in production, etc).

Yes - goto is an ugly construct, but it does have its uses, but only for "experts" (to some extent).

:D

@n46130 Seems you wanted to:

// digitalWrite (13, HIGH);

Leave a line in the code for eventual later use, but don't execute it. A comment is very nice for this purpose.

Hmmm... goto... That rings a bell...

Oh yes... the last time I used goto...

10 PRINT "Hello world"
20 GOTO 10

I think I was 6 at the time, and the computer had rubber keys.

There is simply no place for goto in todays world.

Unless you're talking assembly language, when it's not goto but relative or absolute jumps.