# Matlab What is wrong with this WHILE loop?

Tags:
1. Aug 26, 2016

### ecastro

Here is my code:

Code (Text):
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.

2. Aug 26, 2016

### 256bits

What does the expression ( 1 OR 0 ) give you?

3. Aug 26, 2016

### Staff: 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.

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.

4. Aug 26, 2016

### Image Analyst

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

Code (Text):

% 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

5. Aug 27, 2016

### Staff: Mentor

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 (Text):

while a ~= 3 && b <= 1;
a = a - 1;
(other expressions);
end;
Note the change from "||" to "&&" in the while loop header.

6. Aug 30, 2016

### ecastro

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.

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.

7. Aug 30, 2016

### Staff: Mentor

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

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.

8. Aug 30, 2016

### ecastro

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