goto statements in arduino.

im a experienced basic programmer and ive been wondering what the syntax for a goto statement is in the arduino code ... i basic the goto statement is used in this format
:#this would be used to make a loop
:lbl 1
:
:goto 1

:lbl 1
:digitalwrite(2,1)
:if digitalread(2) = 1 then goto 1
:dgitalwrite(2,0)
:#this would cause the led on pin 2 to flash (in basic) by alternating between off and on.

i need to use the goto statement for a enu system for a watch im buiding using the arduino ... it would be used for menus

Do you have a question?

i need to use the goto statement

That statement is almost certainly false.

(What was it Dijkstra said about BASIC? :smiley: )

im a experienced basic programmer

In which case you presumably do not use the GOTO command. Nor do you need to when using the Arduino as there are better ways of managing program flow.

Arduino is C, and C has goto. You can google goto and C for examples;

However - goto is terrible, and should be avoided when writing code. Calling functions, and using the normal flow control statements produce more maintainable, more readable code, and it's easier to find bugs and not write them in the first place.

Almost nobody still uses goto these days (and those that do, and know what they're doing, use it only in special cases) - it was common in the early days of computing, before people had really figured out that it wasn't a good thing. See "goto considered harmful".

"goto considered harmful".

Well, that is somewhat like religions saying eating pork is harmful. Using goto can be fun as it causes some people to get flipped out over it.

brenden_nerd_:
i need to use the goto statement ...

No, you don't.

I've written thousands of lines of code, and hundreds of Arduino sketches. I never use goto in them.

You may want to ... but if you use them, well, you have just increased your debugging workload.

zoomkat:
Well, that is somewhat like religions saying eating pork is harmful. Using goto can be fun as it causes some people to get flipped out over it.

No, we don't "flip out". We try to help other people do their job using the most efficient tools. That upsets some people.

brenden_nerd_:
:lbl 1
:digitalwrite(2,1)
:if digitalread(2) = 1 then goto 1
:dgitalwrite(2,0)
:#this would cause the led on pin 2 to flash (in basic) by alternating between off and on.

i need to use the goto statement for a enu system for a watch im buiding using the arduino ... it would be used for menus

While "goto" is possible in C/C++ within the same function, nobody uses that.

In case you want to do some BASIC like programming in C/C++:

  • label names must start with a letter
  • uppercase and lowercase is essential, so "digitalread" is something different than "digitalRead"
  • comparisons have to be done with "==", because "=" is always an assignment for a value in C.
  • each command ends with a semicolon ';'

So your BASIC-to-C translated code would probably look like:

void loop() {
label1:
digitalWrite(2,1); 
if (digitalRead(2) == 1) goto label1;
digitalWrite(2,0);
}

BTW: For flashing the LED the code is completely wrong. And if it were working, the flashing would be much too fast for watching it: The LED would switch more than a hundredthousand times per second then. Perhaps add an 'delay(1000)' at some place, after fixing the goto logic for actually doing some switching.

If you were a C programmer you would do endless switching an LED state on/off as fast as possible, more than a hundredthousand times per second so you cannot watch this with your eyes:

void loop() {
digitalWrite(2,!digitalRead(2));
}

You simply read the pin-2 state, and write the inverted/negated state back to the same pin.

DrAzzy:
However - goto is terrible, and should be avoided when writing code.

I thought Brenden Nerd might be having a bit of a troll, as it's Friday.

Almost nobody still uses goto these days (and those that do, and know what they're doing, use it only in special cases)

You realise Brenden proposed [a menu] one of the very few use cases where GOTO might be justified.

  • it was common in the early days of computing, before people had really figured out that it wasn't a good thing. See "goto considered harmful".

It was common in the days of interpreted BASIC on 8 bit micro-computers, along with line numbers. More to do with a lack of the resources to maintain a stack than knowing what was good for us. By the time the BBC Micro launched (1982), with properly structured BASIC, I was already moving on to Pascal.

MattS-UK:
I thought Brenden Nerd might be having a bit of a troll, as it's Friday.
You realise Brenden proposed [a menu] one of the very few use cases where GOTO might be justified.
It was common in the days of interpreted BASIC on 8 bit micro-computers, along with line numbers. More to do with a lack of the resources to maintain a stack than knowing what was good for us. By the time the BBC Micro launched (1982), with properly structured BASIC, I was already moving on to Pascal.

Can't see how a menu justifies a goto.

BASIC pre-dates the BBC micro by nearly two decades, and even eight bit microcomputers by a decade.
Forth also pre-dates 8 bit micros, and quite happily managed a stack.

(As I saw this thread I promised myself not to intervene: soul is strong but flesh is weak . . . .).

When I got my degree (many, many years ago) I hardly distinguished a computer from a chocolate tablet. Then I started working for an electronics company that was making its first steps into (then was called so) microelectronics. Needless to say that it was love at first sight.

The first steps were groping (I've just googled spanish "a tientas": ¿is that -groping- correct?): I used assembler, so I had an assorted collection of instructions to "go to" wherever I wanted to (backwards, forwards and even sidewards) . Needless to say again that the first pieces of code I wrote deserve to be in the horrors house of software. God has forgiven me finally.

I had the insight that something was wrong, but I had nothing to give me some light (Spain was -and, in certain way keeps being- a wasteland in what concerns technical matters; google didn't exist for that historic period :frowning: ). Finally a semi-technical magazine (I think I remember it was hobby oriented) fell in my hands: it said -taught- about structured programming!. From then, it became a paradigm, a beacon that lighted my way: The code I've wrote since that occurence is perfectly structured. I wont explain the rest of the story; another day, perhaps.

My dear friends: one can write perfectly structured programs using "goto" (I don't use it, by the way); in fact "for-next" loops are, in certain way, a sophisticated way of using "goto" and is widely used in code published in this forum (up to now I haven't seen any criticism about it). Some other instructions that are accepted as orthodox (return, exit) are used too.

So, What is the problem of using "goto"?

Best regards.

MattS-UK:
I thought Brenden Nerd might be having a bit of a troll, as it's Friday.
You realise Brenden proposed [a menu] one of the very few use cases where GOTO might be justified.
It was common in the days of interpreted BASIC on 8 bit micro-computers, along with line numbers. More to do with a lack of the resources to maintain a stack than knowing what was good for us.

The only reason for GOTO in ancient BASIC dialects was the fact, that while the language when created first did know only about IF, but it did not know about ELSE, and there was ONLY ONE COMMAND possible after an IF condition.

So when coding an IF-branch, you arranged it like that to simulate IF...ELSE (which was not available):

100 IF condition THEN GOTO 150
120 (else branch code)
130 (else branch code)
140 GOTO 200
150 (if branch code)
160 (if branch code)
170 (if branch code)
200 ...

GOTO was only needed for BASIC dialects that:

  • provided only IF, but no ELSE
  • allowed just one command to execute after an IF condition
    I think Tiny Basic was such a programming language. It fitted within 2 or 3 KB of RAM. Nowadays you can put a "Tiny Basic" interpreter into the RAM of an Arduino board.

So, What is the problem of using "goto"?

The problem is that it can end up, whilst not exactly encouraging, supporting the construction of spaghetti code.
At assembler level, generally, you don't have the choice - goto is the goto solution, but that doesn't mean that you can't think in higher-level constructs.

(just like ex-smokers are the most vehement anti-smokers, those of us damaged by being taught BASIC and other low-level languages (anyone remember CESIL?) early and thinking that goto is the only way of directing program flow, are the most vehement anti-goto-ers.

My road to Damascus didn't come until I studied formal grammars and compiler construction (and reading Michael Jackson's "Structured Programming"), when I finally saw the symmetry of structured methods, and the light.)

I do use "goto" - I've written it three times in the last eight years, all in the context of device drivers.
Two occasions required a review board to authorise.

AWOL:
Can't see how a menu justifies a goto.

Imagine you want to implement a many nested menu system on your Arduino, with every node returning directly to a 'home' screen. Using structured code, the execution point descends a series of functions, each one pushing pointers onto the stack, which grows upwards consuming SRAM. The stack then has to be collapsed, to return to the home screen. Write the menu using GOTOs avoids growing the stack and the CPU overhead of collapsing it.

Essentially, a GOTO compiles to an assembly language BRA (branch always) instruction. An unconditional branch uses fewer clock cycles and less RAM, than pushing data onto the stack and calling JSB (jump subroutine).

Interestingly, Return To Basic was developed for the RasPI by a long time friend of mine. For the sake of nostalgia, I wrote an ASCII arcade scroller for the early version. Initially using line numbers and goto, then again using structured code. The structured code was so much slower, the game became unplayable.

BASIC pre-dates the BBC micro by nearly two decades, and even eight bit microcomputers by a decade.

Yes but the BBC Micro introduced structured BASIC to the masses. BASIC with procedures, functions, local variables and without line numbers. Fast BASIC and GW BASIC soon followed.

Forth also pre-dates 8 bit micros, and quite happily managed a stack.

How many 8 bit home micros came with Forth exactly? One as far as I know, the Jupiter ACE.

The problem is that it can end up, whilst not exactly encouraging, supporting the construction of spaghetti code.
At assembler level, generally, you don't have the choice - goto is the goto solution, but that doesn't mean that you can't think in higher-level constructs.

That's the point: From our latin (mediterranean, I mean) point of view, just forbbiding something is sufficient to prevent the artermaths to occur . . . Nope!: The point is teaching people to follow traffic laws (not to go over speed limits) because it is dangerous, not just because you are going to be penalized. Just removing "goto" or similars will not prevent from writting bad code (I doubt, even, if it is better -to prohibite it- to make people to write code properly).

Best regards

goto will most certain corrupt the stack (and heap) when jumping outside your scope { } or inside a nested scope. if used on the same scope level it is pretty harmless and blazingly fast.

Code with goto can always be rewritten to a goto free version, sometimes with a performance penalty.

my 2 cans of oil :wink:

Hi,

As jurs has said;

The only reason for GOTO in ancient BASIC dialects was the fact, that while the language when created first did know only about IF, but it did not know about ELSE, and there was ONLY ONE COMMAND possible after an IF condition.

My first language was HP Basic in 1973/74, it had no IF..Else and if I remember no CASE....IF Switch, which also helps replace the GOTO.

Tom........ :slight_smile:
PS.. the ancient BASIC dielects, dinosaurs had not become extinct yet.. :grinning:

well by the looks of it im able to use the goto statement ... but ill end up looking like a fool ... which ive already done

well by the looks of it im able to use the goto statement ... but ill end up looking like a fool ... which ive already done

Don't worry . . you will always have my unconditional support :stuck_out_tongue_closed_eyes: