QUESTION!

I did a lot of calculator programming the past few years.
In calculator programming, you can make a part of a command skip to another loop, and it would look something like this

Lbl A
    Disp "Cheese"
    If X=5; Goto B
Goto A
Lbl B
    Disp "Queso?"
Goto B

How Do I do that in Arduino Programming?!?

This should do it...

do
{
  Serial.println( "Cheese" );
}
while ( X != 5 );

while ( true )
{
  Serial.println( "Queso?" );
}

Coding Badly

God Member

You sure live up to that god member title. Thanks man.

No...actually a God Member would do this:

int x=0;

void setup() {
}

void loop() {
  cheese:
    Serial.println( "Cheese" );
    x++;
    if (x != 5) goto cheese;

  queso:
    Serial.println( "Queso?" );
    goto queso;
}

Yes it compiles (as to whether it works or not, I haven't tried); and yes, "goto" is a valid C statement.

Please don't ever write code like this, unless you really know what you are doing...

/I've probably just committed some cardinal sin by introducing the Arduino masses to the goto statement...

;D

Well, that bit of code passed the "verify" test. Nice work. You know, teaching arduino programmers goto commands is one of the 796.5 deadly sins. I believe it is number 652.

Maybe I should be made the first "Evil God Member"...? Or maybe the first BGMFH...?

;D

Only gods banished to the deepest recesses of Hell would ever write "goto" in an application*

  • This restriction does not apply to systems programmer gods.

Please don't ever write code like this, unless you really know what you are doing...

if you really know what you are doing, you wouldn't do it :wink:

/I've probably just committed some cardinal sin by introducing the Arduino masses to the goto statement...

Oh it's been discussed before, trust me. Usually with a zillion repeated posting (from mostly low count posters) saying never, never, ever, ever use it, it's evil, evil.

Then once in a blue moon some old sage will quietly explain that it is not evil if used as designed for special situations (error recovery routines are often stated). Of course such reason is usually lost in the extremely low S/N ratio of such threads.

Lets put it this way, if it was pure evil why would the K&R gods designed it into the C language? Certainly as used in the classic Basic language it breaks the rules of structured programming. But dammit, when one really needs a 10 pound sledge hammer, it's nice to know there is one close at hand in the tool box.

Lefty

I promise never to tell anyone about setjmp.

D'Oh!

Then once in a blue moon some old sage will quietly explain that it is not evil if used as designed for special situations (error recovery routines are often stated).

An old sage that is using goto instead of exception handling for error recovery must be very old indeed.
Alas, exception handling isn't supported in avr gcc, but I doubt that any good would come from Arduino users trying to use goto to recover from errors.

As K&R say in The C Programming Language:
“C provides the infinitely–abusable goto statement. Formally, the goto is never necessary.”

I think they only put the construct in the language to provide a talking point for bored old sages

I use it sometimes, and the world has not stopped spinning yet. :slight_smile:

But every time you use a "goto", a fairy dies (or so my Mum told me), you nymph-murderer you

I think they only put the construct in the language to provide a talking point for bored old sages

Could be, I haven't used it as yet. C is still pretty new to me and anyway hardware is where my heart and experience lies rather then software. However I'm always a little suspicious with people using near religious arguments (have faith, goto is evil) when discussing a technical point.

Then again I'm probably just as bad telling new comers never to use LEDs without resistors, or powering servos directly from a Arduino is evil, evil I say. :wink:

Lefty

Wow. Arduino programmers are wierd people.

I would say programmers in general are "weird people", Buzzsushi. And the closer we get to the hardware, the weirder we become (my closest hardware experience was doing hand-assembly of 6502 assembler in the monitor program on the Apple IIe - that was fun; I hope to in the future replicate that experience when I get my Altair refurbished and working - should be fun to toggle in test code!).

For instance, take Ada Lovelace; she is considered the world's first programmer (and has a programming language named for her), she was a babe (for her time - hell, even today), and sadly died young:

Her hardware: Babbage's Analytical Engine

Her software: Code to compute Bernoulli numbers

She wrote the software "blind" - that is, she had no hardware to test it on, only what she knew of Babbage's machine, her notes, her notes on the algorithm, etc.

This was in 1843; it wasn't recognized exactly what she had accomplished until 1953 - over a century later after her death in 1852.

It's a pity that more women don't know about this amazing lady, and as a consequence go into the more technical arts. Of course, there's always the founder of AdaFruit:

So there's hope...

:slight_smile:

[edit]Kinda left something off - what I was meaning to mention about Ada Lovelace, as related to "weird programmers":

  1. Victorian-era Britain
  2. Female
  3. Skilled in mathematics (!)
  4. Programmed a computer (!!)
  5. That didn't exist (!!!)

For a mid-19th century woman, this is extraordinary; this is way before women's lib, and right in the middle of the beginning of the women's suffrage movement. Her actions would've been seen as a curiousity at best, scandalous at worst; the fact that her work wasn't recognized for what it was until a century after her death speaks volumes.

If that doesn't scream "weird programmer", I don't know what does (though I sincerely doubt she saw herself that way)...[/edit]

Wow. Arduino programmers are wierd people.

We pride ourselves on it....... :slight_smile:

Oh yeah, and goto statements only allow you to skip to other labels within the void loop().....

and goto statements only allow you to skip to other labels within the void loop(

Close...but no cigar.

An old sage that is using goto instead of exception handling for error recovery must be very old indeed.
Alas, exception handling isn't supported in avr gcc, but I doubt that any good would come from Arduino users trying to use goto to recover from errors.

As K&R say in The C Programming Language:
“C provides the infinitely–abusable goto statement. Formally, the goto is never necessary.”

I think they only put the construct in the language to provide a talking point for bored old sages

K&R are stating that goto isn't a requirement in order for the language to be effective, however you're mispresenting K&R's stance by merely quoting only what you did. It's been a long time since I've seen the white bible, but I seem to recall there being two examples where a goto was deemed fine, one was to break out of a doubly nested loop and the other possibly dealing with exceptions.

goto is like anything else in C, it can be abused. Just like pointers.
As always, its best to write things in the least nasty way possible, sometimes that involves a goto.