How do you make a label

Why doesn’t this work?
How do you define a label?
Can you put a label anywhere?

If the label was defined correctly,
would the goto work?

I just wanted to see what would happen.
There is no good Q&A about the goto
without all the scolding.
I know the goto statement is a demon,
or a magic genie that’s bad, bad, bad.

Thanks if you answer.

Avoid all of your questions by simply not using goto

Why do you want to when there is no need ?

please don't post images of text.... just copy & paste the text using code tags

your issue is that the label needs to live inside the current function says the spec (and esp. not in the middle of nowhere)

The goto statement unconditionally transfers control to the statement labeled by the identifier. The identifier shall be a label (6.1) located in the current function

your other issue is probably more serious, you need to understand that the C++ code is not executed top to bottom in your file... it's a succession of function calls and in the arduino environment first setup() is called once and then loop() repeatedly for ever (ie when the code reaches the end of the loop() (and goes back to the main() function) it's being called again soon thereafter)

label and goto are archaic

They have not been valid since structured programming came about in the early 1960s.
If you want to repeat code, you can use loops or recursion.
If you want to repeat based on a deterministic situation, such as doing something 3 times, or one time for each element in an array, or the like, then you want to use a for loop.
If you want to repeat something based on a non-deterministic situation, such as do this until the user enters 0, then you want to use a while loop.

In addition to what others have said...

First you would not want to call setup from the loop(). In this instance, the Arduino IDE makes code so the void setup() will run before the loop().

However ignoring the above statement you would use..

delay(2000);
setup();

The above will "call" the function "setup". So no label is necessary.

Labels are fine for whatever reason you want to use them, but you need to read the language reference.

Your current usage just won’t/can’t work.

Labels have to be inside a function.

You did it right, except that is has to be inside a function.

If the label was inside the same function as the goto, it should work.

What would you want to happen? It can probably be done some other way.

1 Like

Thank you John. You’re very, very, worth talking to. And btw, this is the way you answer a question. M-thanks.

I did what JohnRob suggested,
and nothing noticeably happened.

I was trying to get the serial monitor to reset.
And just have a single line printing out.
I just wanted to see if it would do it. It doesn’t.

It seems that you can re-set a pic by pulling a
pin low or high, if I recall. And I was always
wondering if just randomly resetting and reloading
the program would be a way to ensure stability,
or defeat intruders and others.

I posted the image because it showed the error
that I was asking about. Who would be disappointed
for not being able to copy this code? You know who…

And what’s the harm in using a goto-label, seated
at your desk trying to demonstrate something?
Nothing, that’s what.

that’s all

That was not the point in this case
I answered from my iPhone and the image is super tiny, I don’t see very well at my age and that was making it difficult to read
Posting an image of text costs more resources on the server for storage and from everyone downloading it, aggravating the world’s energy bill and contributing to climate change…

A soft reset could possibly be a goto the start of the code with a
asm volatile("jmp 0x00");
but that won’t reset ports

2 Likes

No, Re-running 'setup()' would not clear Serial Monitor. As far as I know, there is no way for a sketch to clear Serial Monitor. You could print a bunch of empty lines so all the text scrolls up and off the top of the window (if auto-scroll is enabled) but that's about it.

Do you mean on a display connected to the Arduino? Usually there is a way to clear such a display.

I was. If I had been able to copy the code I could have provided an example of a way to do what you seemed to want to do without using a label. Also, on my laptop, a screen capture of error messages comes out as tiny dark red letters on a black background which are quite hard to read.

1 Like

BTW

That highlights the misconception I was hinting at. The start of the file is not the start of a program

There is even a button in the IDE that allows you to copy it with a single click

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.