c++arduinotinkercad

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


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;
    }
}
}

Solution

  • 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