How to get out this Do-While loop?

break while loop java
do-while break c#
do while break javascript
break do while loop java
continue in do while loop
break while loop python
matlab break out of while loop
c how to break a while loop

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?

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.

break in do while loop, The purpose the break statement is to break out of a loop early. For example if the following code asks a use input a integer number x. If x is divisible by 5, the� I need help trying to get out of a “Do While” loop. I have an activity that looks through a long list of ticked items, it unticks all ticked items and ticked wanted items. Ones this is done, my automation seems to be stock in a loop that I don’t know how to get out off. Working examples will be very helpful

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!

Break Statement & Do While Loop, CODE EXAMPLE You can write a do-while or repeat-until loop in Go with a for statement. In the do-while loop, the body of a loop is always executed at least once. After the body is executed, then it checks the condition. If the condition is true, then it will again execute the body of a loop otherwise control is transferred out of the loop. Similar to the while loop, once the control goes out of the loop the statements which are

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...

Terminate execution of for or while loop - MATLAB break, Types of Loops; While Loop; Do-While loop; For loop; Break Statement Once the condition becomes false, the control goes out of the loop. As soon as the code executes the ‘Exit Do’ line, it comes out of the Do While loop and passes the control to the next line right after the loop. For example, if you want to enter the first 10 dates only, then you can exit the loop as soon as the first 10 dates are entered.

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.

Do while loop, A While Loop/Do Loop, must have a condition to met. Try to do a If when I can add the lines, but can't figure out how to make it work. image Because do while loops check the condition after the block is executed, the control structure is often also known as a post-test loop. Contrast with the while loop, which tests the condition before the code within the block is executed, the do-while loop is an exit-condition loop. This means that the code must always be executed first and then

2 patterns for a do-while loop in Go � YourBasic Go, In this tutorial, you will learn to create while and dowhile loop in C To learn more about test expression (when the test expression is evaluated to true and false), check out relational and logical operators. Here, we have initialized i to 1. The program, then enters the body of do..while loop without checking any condition (as opposed to while loop). Inside the body, product is calculated and printed on the screen. The value of i is then incremented to 2.

C Loops: For, While, Do While, Break, Continue with Example, The break statement provides you with the opportunity to exit out of a while loop when an external condition is triggered. You will put the break statement within� Here, the dowhile loop continues until the user enters a negative number. When the number is negative, the loop terminates; the negative number is not added to the sum variable. Output 2. Enter a number: -6 The sum is 0. The body of the dowhile loop runs only once if the user enters a negative number.

How to break a While loop/Do while loop - Build, If you want to exit from the 'middle' of the loop, there is a technique I learned in an old programming class called priming. Basically, you perform some task out of the loop and again at the end of the loop, just before the condition is checked.

Comments
  • Suggested advice: Reduce the number of condition tests in the body of the loop to as few as you really need to test. You can carry an assignment in the body of the loop, but it is not recommended (in case of programmer error). In general, refactor your tests to spot the condition that forces you to quit the loop. This is the condition that you should test against.