当for循环内遇到if语句时跳过for循环
我在 for
循环内有一个 if
语句。当 if
语句中的条件为 true 时,我想跳出 for
循环。
这就是我所拥有的,
if (data < voltage && data2 < voltage) {
digitalWrite(pump, HIGH);
digitalWrite(valve, HIGH);
digitalWrite(valve2, HIGH);
for (int i = 0; i < 10; i++) {
lcd.setCursor(0, 2);
lcd.print("S_1:");
if (data > 260 && data < 295)
lcd.print("MID");
else if (data < 260)
lcd.print("LOW");
else
lcd.print("HIGH");
lcd.setCursor(7, 0);
lcd.print("Pump:ON");
lcd.setCursor(0, 1);
lcd.print("V1:ON");
lcd.setCursor(9, 2);
lcd.print("S_2:");
if (data2 > 260 && data2 < 295)
lcd.print("MID");
else if (data2 < 260)
lcd.print("LOW");
else
lcd.print("HIGH");
lcd.setCursor(7, 1);
lcd.print("V2:ON");
lcd.setCursor(14, 1);
lcd.print("V3:OFF");
sum += data;
sum2 += data2;
delay(1000);
}
average = sum / 10;
average2 = sum2 / 10;
if (average > voltage || average2 > voltage) {
digitalWrite(pump, LOW);
digitalWrite(valve, LOW);
digitalWrite(valve2, LOW);
}
sum = 0;
sum2 = 0;
lcd.clear();
}
它似乎不会抛出整个条件。
I have an if
statement inside a for
loop. When the the condition in if
statement is true, I want to break out of the for
loop.
here is what exactly I have
if (data < voltage && data2 < voltage) {
digitalWrite(pump, HIGH);
digitalWrite(valve, HIGH);
digitalWrite(valve2, HIGH);
for (int i = 0; i < 10; i++) {
lcd.setCursor(0, 2);
lcd.print("S_1:");
if (data > 260 && data < 295)
lcd.print("MID");
else if (data < 260)
lcd.print("LOW");
else
lcd.print("HIGH");
lcd.setCursor(7, 0);
lcd.print("Pump:ON");
lcd.setCursor(0, 1);
lcd.print("V1:ON");
lcd.setCursor(9, 2);
lcd.print("S_2:");
if (data2 > 260 && data2 < 295)
lcd.print("MID");
else if (data2 < 260)
lcd.print("LOW");
else
lcd.print("HIGH");
lcd.setCursor(7, 1);
lcd.print("V2:ON");
lcd.setCursor(14, 1);
lcd.print("V3:OFF");
sum += data;
sum2 += data2;
delay(1000);
}
average = sum / 10;
average2 = sum2 / 10;
if (average > voltage || average2 > voltage) {
digitalWrite(pump, LOW);
digitalWrite(valve, LOW);
digitalWrite(valve2, LOW);
}
sum = 0;
sum2 = 0;
lcd.clear();
}
It seams not going throw the whole conditions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用break关键字。
另外,OOD 人们认为这是 gotoish 代码,更喜欢在 for 条件中插入中断条件:
You could use the break keyword.
Also, this is considered by OOD people a gotoish code, prefer inserting the break condition in the for condition :
使用
break;
关键字来做到这一点。Use the
break;
keyword to do that.