// goto.ino
// Reason to avoid goto statement:
// makes the logic of the program complex and tangled;
// harmful construct and a bad programming practice;
// How to avoid goto statement:
// Try use of break and continue statements.
void doSomething() {
Serial.println("done");
// goto jumpLabel; // error
}
int main() {
// setup
init();
Serial.begin(9600);
// loop
for (;; delay(5000)) {
/* ----------------------------------- */
// goto: jump to a label;
int x = 1;
Serial.println(x); // 1
x = 2;
goto jumpLabel;
Serial.println(x);
x = 4;
Serial.println(x);
jumpLabel:
Serial.println(x); // 2
x = 8;
Serial.println(x); // 8
goto insideAnotherScope;
x = 16;
/* ----------------------------------- */
// label ignores block scope
{
//insideAnotherScope:
int x = 32;
Serial.println(x);
insideAnotherScope:
Serial.println(x); // 64 error flow!
x = 64;
Serial.println(x); // 64
}
/* ----------------------------------- */
}
return 0;
}