Should I use "goto" or something elese?

I’m trying to get this sketch to test a variable and if it’s changed go back or “go to” a previous line and repeat a process.

First time posting little programming experience so bear with me…

Here is the seemingly relevant part:

if (Pot0 != H2OtPast) {
bailout1; //this part the compiler is saying “not declared in this scope” (what does that mean)
lcd.clear();
lcd.setCursor (0,0);
lcd.print(“Watering Time”);
lcd.setCursor (0,1);
lcd.print(H2Ot);
lcd.print(" minutes");

H2OtPast == Pot0;

if (Pot0 != H2OtPast){ goto bailout1;}

Here is the sketch in full:

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR 0x27
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7

LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

int H2OtPast;
int ThreshPast;

void setup()
{
lcd.begin (16,2);

// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home
}

void loop()
{
int Pot0 = analogRead(A0);
int Pot1 = analogRead(A1);

float H2Ot = Pot0 * (10.0 / 1023.0);
float Thresh = Pot1 *(100 / 1023.0);

if (Pot0 != H2OtPast) {
bailout1;
lcd.clear();
lcd.setCursor (0,0);
lcd.print(“Watering Time”);
lcd.setCursor (0,1);
lcd.print(H2Ot);
lcd.print(" minutes");

H2OtPast == Pot0;

if (Pot0 != H2OtPast){ goto bailout1;}

}

if (Pot1 != ThreshPast) {

bailout2;
lcd.clear();
lcd.setCursor (0,0); // go to start of 1st line
lcd.print(“ThreshHold”);
lcd.setCursor (0,1); // go to start of 2nd line
lcd.print(Thresh);
lcd.print(" %");

ThreshPast == Pot1;
if (Pot1 != ThreshPast){ goto bailout2;}
}
lcd.clear();
lcd.setCursor (0,0); // go to start of 1st line
lcd.print(“Watering Time”);
lcd.setCursor (0,1); // go to start of 2nd line
lcd.print(H2Ot);
lcd.print(" minutes");
delay(2000);

lcd.clear();
lcd.setCursor (0,0); // go to start of 1st line
lcd.print(“ThreshHold”);
lcd.setCursor (0,1); // go to start of 2nd line
lcd.print(Thresh);
lcd.print(" %");
delay(2000);
}

Thanks!

Should you use goto? Absolutely not.

    bailout1; //this part the compiler is saying "not declared in this scope" (what does that mean)

It means that labels are defined with colons, not semicolons.

    H2OtPast == Pot0;

This is a comparison, not an assignment.

That snippet should be a while statement, not an if statement. It doesn't need a goto.

From K&R:

C provides the infinitely-abusable goto statement, and labels to branch to. Formally, the goto is never necessary, and in practicve it is almost always easy to write code without it. We have not used goto in this book.

Nevertheless, there are a few situations where gotos may find a place. The most common is to abandon processing in some deeply nested structure, such as breaking out of two or more loops at once. The break statement cannot be used directly since it only exits from the innermost loop.

And in fact that is the only place I have needed to use goto, during error recovery in a tangled set of loops.

KeithRB:
From K&R:

… and who are we to argue :wink:

Well, they weren't perfect: http://www.lysator.liu.se/c/dmr-on-or.html

A bit like the gag that when God made Man, She was only joking, K&R confess

The only technical issue with "goto" is when it is used inappropriately used in code just like any other code functions. Most people whine about "goto" because they probably can't wrap their head around the code logic flow. At times it is more code efficient to use "goto" than the alternative code processes. YMMV

zoomkat: The only technical issue with "goto" is when it is used inappropriately used in code just like any other code functions. Most people whine about "goto" because they probably can't wrap their head around the code logic flow. At times it is more code efficient to use "goto" than the alternative code processes. YMMV

If you've ever written a compiler backend you'll laugh at this - gotos are much harder to optimize than structured control statements since there are no hints as to the structure for the optimizer to use.

Normally you'd point your optimizer hard at the innermost for and while loops first, which is where the low-hanging fruit usually is. With goto / spaghetti code there's no such thing as "innermost loops" for the compiler to identify - its important the optimizer optimizes the right bits of the code, since optimizing one part of the code often pessimizes other parts.

For instance most high performance compilers for numerical processing do loop-unrolling and operator-strength-reduction which is harder to get right with just a bunch of random basic blocks. Graphs are opaque/low-level trees are high-level.

Full blown C++ has exceptions and they exactly the right thing to use for escaping on error conditions. In C you have longjmp for this purpose, clunky though it may be.

Change "bailout;" (with a semicolon) to "bailout:" (with a colon) and it should work. Simple typo, that's all.