如何摆脱这个 Do-While 循环? [英] How to get out this Do-While loop?
问题描述
我有这个基本的 Arduino 代码,我想要 2 个选项来退出这个 Do-While 循环.我简化了我的原始代码以突出真正的问题:Do-While 不识别 OR ||退出循环的条件
I've this basic Arduino code, and I want to have 2 options to exit this Do-While loop. I simplified my original code to highlight the real problem: the Do-While doesn't recognize the OR || condition to exit the loop
在这段代码中,我从两个等于 0 的整数变量开始,一旦它们进入 Do-While,它们将被设置为等于 2,以便它们可以立即退出 Do-While在第一次迭代中.
In this code I'm starting with two integer variables that are equal to zero and as soon as they enter the Do-While, they'will be setted equal to 2 both, so that they can immediately exit the Do-While in the first iteration.
这是我的代码:
int fin = 0;
int ending = 0;
int counter = 0;
void setup() {
Serial.begin(9600);
}; //void setup
void loop () {
do {
Serial.println("We are IN Do-While #1");
ending = 2;
//fin = 2;
} while ((ending < 1) || (fin < 1)); //I have just one condition for exit the Do-While ending = 1
Serial.println("We are OUT Do-While #1");
delay(3000);
do {
counter++;
} while(counter<=100);
Serial.println("We are OUT Do-While #2");
delay(3000);
}
我的问题是我没有设置 fin = 2,因为我想测试 OR 条件是否有效.
My problem is that I'm not setting fin = 2, because I want to test if the OR condition is working.
但它似乎不能退出 Do-While 除非它们都等于 2.这对我来说很奇怪,因为 OR 条件允许使用双重选项退出 Do-While,在这种特殊情况下这些选项是:
But it seems that it can't exit the Do-While unless they're both equal to 2. This is strange to me because the OR condition allows to exit the Do-While with a double options, in this particular case these options are:
结尾<1 OR(替代选项).. fin<1
ending<1 OR (the alternative option).. fin<1
此外,如果我更改(其余代码相同)带有 AND 的 while 条件,它的行为就像我想要的:这样我就有两种方法可以退出 Do-While 循环.像这样:
Additionally if I change (the rest of the code is the same) the while condition with an AND it behave like I want: so that I have two ways to exit the Do-While loop. Like this:
} while ((ending < 1) && (fin < 1));
但不是在 AND 条件中我必须匹配 ending >= 1 AND (同时)fin >= 1 的 BOTH 条件才能退出循环?
But wouldn't be that in an AND condition I must match BOTH condition of ending >= 1 AND (at the same time) fin >= 1 to exit the loop?
为什么会这样?我该如何解决这个问题?
Why is this happening? and How can I solve this?
推荐答案
但它似乎不能退出 Do-While 除非它们都等于 2.这对我来说很奇怪,因为 OR 条件允许使用双重选项退出 Do-While,在这种特殊情况下这些选项是:
But it seems that it can't exit the Do-While unless they're both equal to 2. This is strange to me because the OR condition allows to exit the Do-While with a double options, in this particular case these options are:
其实恰恰相反.
循环有两个选项可以继续.
我确定你指的是 &&
,而不是 ||
.
I'm sure you meant &&
, not ||
.
但不是在 AND 条件中我必须匹配结束 >= 1 AND(同时)fin >= 1 的 BOTH 条件吗?
But wouldn't be that in an AND condition I must match BOTH condition of ending >= 1 AND (at the same time) fin >= 1?
是的,继续.
这意味着,您只需要不匹配一个即可停止.
Which means, you only need to not match one of them, to stop.
这篇关于如何摆脱这个 Do-While 循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!