# What is wrong with this WHILE loop?

• MATLAB

## Main Question or Discussion Point

Here is my code:

Code:
while a ~= 3 || b <= 1;
a = a - 1;
(other expressions);
end;
For this code, the 'while' loop should stop when 'a' reaches 3, however it does not, even though 'a' starts with a higher value than 3.

The 'other expressions' part are calculations that manipulate 'b', the only time I manipulate 'a' is at the first line of the loop. So, it should still work due to the 'OR' condition imposed by the '||' symbol. What could be wrong?

I tried removing the second condition and it works, but I cannot remove it here because it is essential to my calculations.

Related MATLAB, Maple, Mathematica, LaTeX News on Phys.org
256bits
Gold Member
What does the expression ( 1 OR 0 ) give you?

mfb
Mark44
Mentor
Here is my code:

Code:
while a ~= 3 || b <= 1;
a = a - 1;
(other expressions);
end;
For this code, the 'while' loop should stop when 'a' reaches 3, however it does not, even though 'a' starts with a higher value than 3.

The 'other expressions' part are calculations that manipulate 'b', the only time I manipulate 'a' is at the first line of the loop. So, it should still work due to the 'OR' condition imposed by the '||' symbol. What could be wrong?

I tried removing the second condition and it works, but I cannot remove it here because it is essential to my calculations.
Your explanation isn't totally clear (with regard to the value of b), but I assume you want the loop to exit when a ≠ 3 OR b > 1.

So you want the loop to perform another iteration if the expression above is not true. IOW, !(a ≠ 3 OR b > 1). According to one of DeMorgan's Laws, ~(p OR q) = ~p AND ~q. Similarly, ~(p AND q) = ~p OR ~q. Here p and q represent logical expressions -- expressions that are either true or false.

Not sure what conditions you want to run the loop or quit the loop, but perhaps this commented code will explain some things:

Code:
% Initialize some values
a = 5;
b = 0.5;
% This following loop will execute forever
% because b is less than 1.
% It doesn't matter at all what a is
% because the b condition is always met.
% while a ~= 3 || b <= 1
%    a = a - 1
% end
% Better is to put a failsafe so we don't get into an infinite loop
maxIterations = 40;
loopCounter = 1;
while (a ~= 3 || b <= 1) && loopCounter < maxIterations;
a = a - 1
loopCounter = loopCounter + 1;
end

b = 5
% This following loop will execute until
% a decrements down to 3 and then quit.
% b is more than 1 but that doesn't matter
% because it enters and continues the loop
% due to the a condition being met.
while a ~= 3 || b <= 1
a = a - 1
end

Mark44
Mentor
Not sure what conditions you want to run the loop or quit the loop, but perhaps this commented code will explain some things:

Code:
% Initialize some values
a = 5;
b = 0.5;
% This following loop will execute forever
% because b is less than 1.
% It doesn't matter at all what a is
% because the b condition is always met.
% while a ~= 3 || b <= 1
%    a = a - 1
% end
% Better is to put a failsafe so we don't get into an infinite loop
maxIterations = 40;
loopCounter = 1;
while (a ~= 3 || b <= 1) && loopCounter < maxIterations;
a = a - 1
loopCounter = loopCounter + 1;
end

b = 5
% This following loop will execute until
% a decrements down to 3 and then quit.
% b is more than 1 but that doesn't matter
% because it enters and continues the loop
% due to the a condition being met.
while a ~= 3 || b <= 1
a = a - 1
end
I don't believe your code solves the problem in the OP. The solution is much simpler, and doesn't require an additional variable. The information from the poster is incomplete, but I think this will fix the problem:
Code:
while a ~= 3 && b <= 1;
a = a - 1;
(other expressions);
end;
Note the change from "||" to "&&" in the while loop header.

Your explanation isn't totally clear (with regard to the value of b), but I assume you want the loop to exit when a ≠ 3 OR b > 1.
I want the loop to end when 'a' is equal to 3 or when 'b' is greater than 1. So, the loop must run if either 'a' is not equal to 3 or if 'b' is still less than 1.

Note the change from "||" to "&&" in the while loop header.
I suppose for the '&&', the two conditions must not be met to exit the loop. However, I just need one of the conditions to not be met to exit the loop.

Mark44
Mentor
Your explanation isn't totally clear (with regard to the value of b), but I assume you want the loop to exit when a ≠ 3 OR b > 1.
ecastro said:
I want the loop to end when 'a' is equal to 3 or when 'b' is greater than 1.
So, based on this, the loop should start another iteration if the opposite of the above compound condition is true. IOW, while ( (a != 3) && (b <= 1)). This is the same as what I said earlier.

I'm using C/C++ notiation. I'm not sure what you're writing code in, matlab?
ecastro said:
So, the loop must run if either 'a' is not equal to 3 or if 'b' is still less than 1.
No, that's incorrect, given what you said above, and this is why your loop isn't working correctly. Look up De Morgan's Laws, which deal with logical expressions joined with AND and OR, and the negations of the same.

Note the change from "||" to "&&" in the while loop header.
ecastro said:
I suppose for the '&&', the two conditions must not be met to exit the loop. However, I just need one of the conditions to not be met to exit the loop.
The expressions in the while loop header aren't about when the loop should exit -- they are about when the loop should start a new iteration.

jim mcnamara
When I think about it that way, the '&&' does make sense. Thank you very much!