The "break statement" is just not working like it should

89 views Asked by At

This is the code but for some reason, when (distancia<100) the ("ALTO") still appears, and the same with the other while loop

while (distancia<200 && distancia>100)
{
    lcd.print("ALTO");
    delay(1000);
    lcd.clear();
    delay(1000);

  if(distancia<100)
  {
    break;
 }
}
 while(distancia<100)
  {
    lcd.print("INTRUSO EN");
    lcd.setCursor(0,1);
    lcd.print("LA PUERTA");
    tone(11,700,250);
    digitalWrite(8,HIGH);
    delay(500);
    lcd.clear();
    digitalWrite(8,LOW);
    delay(500);

    if(distancia>100);
    {
       break;
    }
}
}
2

There are 2 answers

1
micu chi chu On BEST ANSWER

this is your while loop:

while (distancia<200 && distancia>100)

and this is your if:

if(distancia<100);
{
   break;
}

a variable can't be greater and smaller than 100 at the same time so that if is basically useless

0
Adilet Soronov On
while (distancia<200 && distancia>100)
{
    lcd.print("ALTO");
    ...
  if(distancia<100)
  {
    break;
  }
}
  1. while condition guarantees that distancia > 100, so your inner if is useless.
  2. Inside the loops you don't change distancia meaning the loops are infinite - seems like a bug to me.