HOW TO USE GOTO STATEMENT IN ARDUINO

hi guys iam new to arduino and its sketch .but iam a intermediate level in electronics and c coding but here iam confused . any one know how to use goto statement ,ineed a certain point of my code so give me an example

If you want to progress beyond intermediate level, I’d suggest you forget goto even exits in C/C++.

Your question begs for some advice – that I’d suggest would be: “DO NOT USE A GOTO STATEMENT.”

A far better alternative is to design, write and test your program using a structured programming approach

If you would like to see an overview of why the “GOTO” statement has been superseded by the development of structured programming, then I suggest a good summary of early work is available at
http://users.csc.calpoly.edu/~jdalbey/308/Resources/StructuredProgramming

As Djixtra and others pointed out, the “GOTO” is unnecessary and leads to programs that are difficult to understand and thus difficult to write with correct functionality and even more difficult to test and debug.

You can probably find some good tutorial by doing a simple web search. Good luck!

Sorry to say, but if you are confused about that, you are not at an intermediate level.

In case you're trying to Google JimB's reference, it's spelled "Dijkstra"

:frowning: Thanks for the correction to my lousy typing! Cheers jb :slight_smile:

xparkz:
but here iam confused

Not only that, but your caps lock stuck on when you wrote the subject line.

The suggests general carelessness. :astonished: Not a useful attribute for a programmer.

xparkz:
hi guys iam new to arduino and its sketch .but iam a intermediate level in electronics and c coding but here iam confused . any one know how to use goto statement ,ineed a certain point of my code so give me an example

Real programmers don't use goto.

But if you need an example, I have one for you.

This function reads the state of a pin, and changes the state from HIGH to LOW or the other way round.

void changePinState(int pin)
// if pin reads LOW, set it HIGH
// if pin reads HIGH, set it LOW
{
  int state=digitalRead(pin);
  if (state == HIGH) goto skip_1; 
  if (state == LOW) goto skip_2;
  goto error;
  skip_1:
  state = LOW; 
  goto done;
  skip_2: 
  state = HIGH; 
  goto done;
  error:
  Serial.println("Internal Function Error");
  done: 
  digitalWrite(pin, state);
}

I tried to show you all the "advantages" of goto in one single function, hehe :smiling_imp:

jurs:
I tried to show you all the "advantages" of goto in one single function, hehe :smiling_imp:

You're not an expert user of GOTO! Your code was much too easy to follow. You need to 'spaghettify ' it by moving all the labels about. :slight_smile:

Hi,

void changePinState(int pin)
// if pin reads LOW, set it HIGH
// if pin reads HIGH, set it LOW
{
  int state = digitalRead(pin);
  if (state == HIGH) goto skip_1;
  if (state == LOW) goto skip_2;
  goto error;
skip_1:
  state = LOW;
  goto done;
skip_2:
  state = HIGH;
  goto done;
error:
  Serial.println("Internal Function Error");
done:
  digitalWrite(pin, state);
}

If you Auto Format it even looks pretty. and logical, bring on GOTO statements I say.
It reads like a story.

It looks okay for quick and dirty code, BUT when you get a bigger and more convoluted sketch, goto and line names become a pain.
Tom.... :slight_smile:
PS You forgot Delay(10000)! ! !

I used a goto recently. It was in some messy menu code. It allows for any combination of the serial monitor line-ending option to work with the menu. It seemed logical at the time. Every so often I screw up the copy-paste when adding a new menu item so that I have two labels with the same name and the compiler yells at me.

Where we’re switch’in(), if’in(), while’ing or do’ing()… we don’t need no stinkin’ goto’s.

ok i forget goto .but any one help me with my project

my project is a …PASSCODE LOCK SYSTEM USING EEPROM MEMORY…

components iam used:-

arduino uno

16X2 lcd

relay module(single channel)

4x4 keypad

so now iam 45% done with my project iam stuck

i know iam a fool in coding but iam a beginer to maker culture .if any maker like to help me please…

xparkz:
ok i forget goto .but any one help me with my project

my project is a .............PASSCODE LOCK SYSTEM USING EEPROM MEMORY...............

...
...

so now iam 45% done with my project iam stuck

So show your code and tells us where you are stuck. Probably better to start a new thread for that.

xparkz:
i know iam a fool in coding but iam a beginer to maker culture .if any maker like to help me please.....

Help with what?

Goto is a prehistoric relict of the first and very limited BASIC interpreter languages from the nineteen-sixties. The "Goto" command has been invented for two reasons at that time:

    • after an if-statement there was just one() command allowed
  1. else-statements were not yet invented

Learning about goto in the 21st century for the reason of programming is apure waste of time.
Except you want to program old computers with built-in BASIC language interpreter from the nineteen-sixties

Help with what?

I believe in his second post he was asking how to do it without using goto.

Goto is a prehistoric relict of the first and very limited BASIC interpreter languages from the nineteen-sixties.

It's even older than that. You can't blame everything on BASIC

AWOL:
It's even older than that. You can't blame everything on BASIC

Right, I used it in FORTRAN.

Who remembers FOCAL?

1 Like