如何摆脱这种“做时循环"? [英] How to get out this Do-While loop?
问题描述
我有这个基本的Arduino代码,我想有 2个选项退出此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
在这段代码中,我从两个等于零的整数变量开始,并且一旦它们进入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.
但是,除非它们都等于2,否则似乎无法退出Do-While.这对我来说很奇怪,因为在这种特殊情况下,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 或(替代选项).. 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 的两个条件才能退出循环?
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?
推荐答案
但是,除非它们都等于2,否则似乎无法退出Do-While.这对我来说很奇怪,因为在这种特殊情况下,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:
实际上,情况恰恰相反.
Actually, it's the opposite.
循环有两个选项继续前进.
我确定您的意思是&&
,而不是||
.
I'm sure you meant &&
, not ||
.
但是不是在AND条件下我必须同时匹配> = 1 AND(同时)fin> = 1的两个条件吗?
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.
这篇关于如何摆脱这种“做时循环"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!