How to get out this Do-While loop?

♀尐吖头ヾ 提交于 2020-06-16 03:00:08

问题


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

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.

Here's my code:

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

  }

My problem is that I'm not setting fin = 2, because I want to test if the OR condition is working.

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:

ending<1 OR (the alternative option).. fin<1

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

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?


回答1:


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.

The loop has two options to keep going.

I'm sure you meant &&, not ||.

But wouldn't be that in an AND condition I must match BOTH condition of ending >= 1 AND (at the same time) fin >= 1?

Yes, to carry on.

Which means, you only need to not match one of them, to stop.




回答2:


Remember that if you say while (condition), you'll loop so long as condition evaluates to true. So since your condition is

(ending < 1) || (fin < 1)

the only way for this to be false is if both ending < 1 is false AND fin < 1 is also false.

A simple trick when you're getting mixed up like this is to use DeMorgan's Law to find the contrapositive. In other words, if you want to loop while (ending < 1) || (fin < 1), that's the same as saying you want to STOP looping when the opposite is true. Using DeMorgan's Law, we can see that this is:

!((ending < 1) || (fin < 1))
!(ending < 1) && !(fin < 1)
ending >= 1 && fin >= 1

So we only STOP looping when ending >= 1 && fin >= 1!

Working the other way, if you want to STOP looping when ending >= 1 || fin >= 1, then we'll loop while the opposite is true. Again working through with DeMorgan's Law...

!(ending >= 1 || fin >= 1)
!(ending >= 1) && !(fin >= 1)
ending < 1 && fin < 1

So you wanted an AND instead of an OR all along!




回答3:


In your current state the loop will continue as long as (at least) one of the conditions is true (it doesnt have to be both!).

as long as ending is smaller than 2 OR fin is smaller than 2 then the loop will continue. In your code, fin is smaller than 2 so the loop continues...




回答4:


So, we are talking about the first loop in the code.

  do  {

  Serial.println("We are IN Do-While #1");
  ending = 2;
  //fin = 2;
} while ((ending < 1) || (fin < 1));

Here if you don't change the "fin" variable it remains the same, and the exit condition will not be accomplished, that causing it not to end.

You could use an if condition it the loop,

  do  {

  Serial.println("We are IN Do-While #1");
  ending = 2;
  //fin = 2;
    if ( ending >= 1 ) break;
  }

Or just use the || operator as you mentioned.



来源:https://stackoverflow.com/questions/59546132/how-to-get-out-this-do-while-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!