Paul__B:
Who remembers FOCAL?
Hell yes. It still amazes me you could do anything useful with interpreter and program in 4K of 12 bits. I even did some simple graphics in FOCAL on a storage tube display.
Paul__B:
Who remembers FOCAL?
Hell yes. It still amazes me you could do anything useful with interpreter and program in 4K of 12 bits. I even did some simple graphics in FOCAL on a storage tube display.
Hi,
AWOL:
Hell yes. It still amazes me you could do anything useful with interpreter and program in 4K of 12 bits. I even did some simple graphics in FOCAL on a storage tube display.
I didn't know they had Sonar on the Ark..... :o
Tom....
TomGeorge:
Hi,I didn't know they had Sonar on the Ark..... :oTom....
Well, you know the old gag about the surgeon, the civil engineer and the programmer arguing about which was the oldest profession (no, not that oldest profession)
Surgeon: "Well, it was us, with that cloning job, taking one of Adam's ribs and creating Eve."
Civil Engineer: "Rubbish, it was us, with that seven day construction job, creating Heaven and Earth from chaos".
"Ah!", says the software engineer, "and just where do you think the chaos came from?"
Like all commands GOTO can be useful if used correctly.
For example the first few lines of the loop might check some switch positions and then execute a block of code. If these blocks are very long then you can get confusion in the indenting.. i.e. pseudo code
Start Loop:
Read Switches
If switch1 = 1 then
{500 lines of code}
end if
if switch2 = 1 then
{ 500 lines of code}
end if
But with GOTO you can do this
Start Loop:
if switch1 = 1 then goto Switch1:
if switch2 = 1 then goto Switch2:
/------------------------------------
Switch1:
{Code}
/------------------------------------
Switch2:
{Code}
which can make the code more readable.
If only they had something like switch case.
Oh they do.
https://www.arduino.cc/en/Reference/SwitchCase
.
After many years in the IT industry, including working with large coding teams that developed Structured Programming techniques, and built million-line complex systems, I would NEVER suggest the use of GOTO.
The key changes of the 1970's were sometimes called GOTO-less programming.
The GOTO verb readily leads to spaghetti-code where logic leaps from one sequence of logic into another without any structure. Sure marcwolf's sample is very constrained and uses only one level of GOTO to transfer from the main loop to subordinate functions, but what happens after that ? I've tried often to diagnose logical errors in code with GOTO scattered about - and it is damnded near impossible.
Look up any text on structured programming - going back to Djikstra and others - from times when my hair wasn't grey - and you'll see why the GOTO was banned by programming standards in many shops.
If subordinate processes are defined as subroutines then the logic of the main routine is simpler and very consistent - you always go from beginning of main loop to the end - no logic errors ! Each routine (main or subroutine) is a discrete set of logic that can be tested and verified independently
void loop (){
read_data();
if .... sub_routine1 ();
else sub-routine2 ();
}
void subroutine1 () {
...
}
void subroutine2 () {
...
}
marcwolf:
Like all commands GOTO can be useful if used correctly.For example the first few lines of the loop might check some switch positions and then execute a block of code. If these blocks are very long then you can get confusion in the indenting.. i.e. pseudo code
Start Loop:
Read Switches
If switch1 = 1 then
{500 lines of code}
end if
if switch2 = 1 then
{ 500 lines of code}
end ifBut with GOTO you can do this
Start Loop:
if switch1 = 1 then goto Switch1:
if switch2 = 1 then goto Switch2:/------------------------------------
Switch1:
{Code}/------------------------------------
Switch2:
{Code}which can make the code more readable.
The GOTO solution above is somewhat over simplified, giving the impression it is tidier than it actually is. It would be more like this and having worked a long time ago in Fortran IV (that is 4 in Roman numerals!) where a simple if then else construct as such didn't exist, I have some experience of this.
Start Loop:
if switch1 = 1 then goto Switch1:
ret1:
if switch2 = 1 then goto Switch2:
goto end:
/------------------------------------
Switch1:
{Code}
goto ret1:
/------------------------------------
Switch2:
{Code}
end:
goto loop:
You shouldn't have "500 lines of code" in an if clause.
jurs:
Learning about goto in the 21st century for the reason of programming is apure waste of time.
What about assembly? It is named jump but is the same thing.
Some time ago I have written code (in C) with for loop nested in another for loop (is there better way to work with 2D arrays?). AFAIK there is no better way than GOTO to break from both loops when a condition is fulfilled. Sure, you can set a flag and check the flag each iteration but it is more confusing than simple GOTO.
It's relatively easy to break out of nested for loops on some exit condition without using goto:
found = false;
for (i=0; i<inum and not found; i++)
{
for (j=0; j<jnum; j++)
{
[do stuff in 2D array]
if (<some condition>)
{
found = true;
break;
}
[possibly do more stuff in 2D array]
}
}
I don't know why we resurrected a year old argument, but your break only breaks the j loop, not the I loop.
aarg:
It's relatively easy to break out of nested for loops on some exit condition without using goto:
KeithRB:
I don't know why we resurrected a year old argument, but your break only breaks the j loop, not the I loop.
Do you see? Such way of breaking from both loops is confusing
Somewhere I have read avoiding GOTO does not make the code well written. You can write cryptic code without GOTO easily. Using GOTO (rarely) may make code more readable - for example when breaking from multiple loops.
Using GOTO (rarely) may make code more readable - for example when breaking from multiple loops.
But, even then, there are ways to avoid it.
bool found = false;
for(byte i=0; i<255 && !found; i++)
{
for(byte j=0; j<255; j++)
{
if(array[i][j] == someValue)
{
found = true;
break;
}
}
}
When found is set to true, the inner loop is exited. The outer loop ends, and the while clause is evaluated again. i is still less than 255, but !found is no longer true, so the outer loop ends.
I use this structure quite often when dealing with all the points on all the ply boundaries in all the sequences in all the ply groups in all the parts in all the products in a document, where I have 5 nested for loops. If the user presses the "Hey, stop, something went wrong" button, I can nearly instantly, with nary a goto anywhere, bail out of all nested loops.
TomGeorge:
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.... :) PS You forgot Delay(10000)! ! !
Hear, hear. I think that no matter your programming skill level either the goto or do_while, do_until, if, etc.
can be coded in such a way as to be un readable or crystal clear.
The right tool at the right time.
I even think Knuth in " The art of Programming " supports my position.
So there !
@PaulS:
Wow!
This is exactly the same code as aarg presented and confused KeithRB. Looks like trying to avoid GOTO generates lot of confusion.
Looks to me like goto generates a lot of hot air.
Actually, you can code such a thing without any "break"...
found = false;
for (i=0; i<inum and not found; i++)
{
for (j=0; j<jnum and not found; j++)
{
[do stuff in 2D array]
if (<some condition>)
{
found = true;
do something about it
}
else
{
[possibly do more stuff in 2D array]
}
}
}
... and I added the else clause for completeness, often you would not need anything there.
I don't see anything confusing about it.
I am not confused.
Depending on the fact that there is no code between the loops is not a "break", and in fact just asking for a bug when you decide you need code there.
Some languages have a break(n) statement to say how many loops should be broken.
KeithRB:
Depending on the fact that there is no code between the loops is not a "break", and in fact just asking for a bug when you decide you need code there.
No matter what the syntax, it is the programmers responsibility to ensure the integrity of the code. There is no language feature that will automatically guarantee correctness.