How to get out this Do-While loop? - c++

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?

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!

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.

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

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.

Related

Why does this while loop keep looping even when its false? In C++

This is the code in question:
int integer;
while (integer != 0 || integer != 1) {
cout << "Choose an integer: \n0\n1\n";
cin >> integer;
}
When I type 1 it continues looping even though the statement is false.
I have had this problem before or similar but it got fixed in a weird way that seems to not be working right now.
The other code that was having problems was this one:
while(chosen != 1 || chosen != 2 || chosen != 3)
{
cin >> chosen;
}
I got it fixed by doing this:
while(chosen < 1 || chosen > 3)
Does annyone know whats happening here? Ty in advance!
let me put you out of your misery
while(chosen != 1 && chosen != 2 && chosen != 3)
{
cin >> chosen;
}
This is a common issue, people translate the human idea in their heads into code: "if its not 1 or 2 or 3 then do xx". But that doesnt work.
(chosen != 1 || chosen != 2 || chosen != 3)
will always be true.
If chosen is say 0 then chosen != 1 is true. So the overall condition is true.
If chosen is 1 (which should be the end of your loop) then chosen !=1 is false, BUT chosen != 2 is true so the overall condition is still true (its true if one of the clauses is true, this is what 'or' / '||' means).
In fact there is no value for chosen which will cause the overall condition to be false. Chosen is always going to not equal one of 1 or 2 or 3.
Your problem came from the looseness of human thought, in conversation we would get what you mean, but not computers. What you wanted was "if its not 1 and its not 2 and its not 3 do xx". Ie
while(chosen != 1 && chosen != 2 && chosen != 3)

Why will my elseif statment never executed

Any idea why the else if statment will be never executed ? The value of difference is constantly changing when the program runs.
double difference = abs(reale_x[0] - reale_x[1]);
if (0 <= difference < 45) {
timer_counter += 1;
if (timer_counter == 30) {
cout << "CLICK" << '\n';
}
}
else if (difference > 50) {
timer_counter = 0;
}
That is not how comparation works in c++.
What this code
if (0 <= difference < 45) {
does is it first compares if 0 is smaller or equal to difference. It is then "replaced" by a bool value either true or false. And then a bool value (so either 1 or 0) is compared to 45. And it will always be smaller than 45. What you have there is an always true statement.
So the way you would write this if statement is
if (difference >= 0 && difference < 45){
Note that because of your else if statement it will not execute if the difference is >44 and <51
if (0 <= difference < 45) will be executed as if ((0 <= difference) < 45), which will be either 0<45 or 1<45 and will always be true. That's why the else part is not getting executed.
in mathematics, we see and write 0 <= x < 45 or something like that to define the range of the variable x. But in order to tell the computer the same thing, you have to tell more clearly. Saying, to have to tell the compiler, that the value of x is greater than or equal to zero and at the same time, that value will be less than 45, and you can tell the compiler by this statement: difference >= && difference < 45 . the && is an 'AND' operator in most of the languages.

How to create a loop that won't stop until the condition is satisfied

I'm new to C++ and before learn C++ I have learned pascal. I have to keep repeat the input process until this condition is satisfied (1 <= m <= n <= 1000000000, n-m<=100000) in pascal it's pretty easy with the "repeat... until" command but in C++ there is only "while" which only stop when the condition is false
Just put your conditions in a while loop separating each condition with && if you want both conditions to result true:
try this:
while(!(m>=1 && n>=1 && n<=1000000000 && (n-m) <=100000)){
// your code here
}
while(true){
if(m>=1 && n>=1 && n<=1000000000 && (n-m) <=100000)
break;
// do something
}

Do while with a boolean as the condition in c++?

so i want to know how a boolean acts in a condition statement in the following code
bool flag = true;
do {
d += data[i];
if (d > 15 || i == 3) {
flag = false;
}
i = i + 1;
} while (flag);
when will it exit the dowhile loop?
If either d > 15 or i == 3 evaluates to true, i will get incremented and the loop will stop.
In other words, flag is only checked at the end of each iteration, even though it might be set to false in the middle of one.
It will exit when (d > 15 || i == 3) which means (d > 15 or i == 3).
i is incremented at each iteration therefore if i is < 3 at the beginning of the program we are sure that at a certain point it will reach i == 3 and break the loop.
On d we can't tell much since we don't know it initial value nor its behavior inside the loop since we don't know anything about data.
Depends on your values of d and i...
As soon as d is greater than 15 or i is equal to 3, flag becomes false and the loop will end.
This might not happen in the same iteration, though. For example if i is incremented to 3 in a loop, it will be evaluated in the following loop first and flag might be set to false.
It will break the while when SUM of first 3 values in array Data[] be greater than "15", or break if does not be greater than 15 the SUM of first 3 values.
[It's depend on initial value of "i"]

If/else if not executing even though it appears conditions are met inside of a while loop

Pretty much what the title says. I'm to the last bit of this lottery numbers assignment, and I'm not sure why the second set of if/else if statements aren't displaying when I debug. I know that if/else statements are mutually exclusive - but shouldn't an if and then another if both test?
Here's the code.
count=0;
while(count<5)
{
if(lottery[count] == user[count])
{
lotto = lottery[count];
cout<<"So, you matched numbers with "<<lotto <<".\n";
tally++;
}
if(tally==5 && count == 5)
{
cout<<"Congratulations, you're the grand prize winner!";
}
else if(tally < 5 && count == 5)
{
cout<<"A total of "<<tally<<" of your lottery picks matched.";
}
else if(tally == 0 && count == 5)
{
cout<<"Caution. The following comment is an inside joke. Read at your own risk.";
cout<<"Bet you feel like a total loser huh? Nothing matched.";
}
count++;
}
I know I probably ought to have replaced the while loop with a for loop for simplicity's sake but i'm more comfortable with the while.
count will never be 5 when the if blocks are executed.
As soon as it becomes 5, the condition fails and the loop stops.
count will never be equal to 5 inside the while loop at the point where your if-else conditions require it. If you had incremented count before the if-else then it would have been possible to satisfy one of the conditions (depending on the value of tally).