What is wrong with this WHILE loop?

  • #1
254
8

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.
 

Answers and Replies

  • #2
256bits
Gold Member
3,180
1,197
What does the expression ( 1 OR 0 ) give you?
 
  • Like
Likes mfb
  • #3
33,722
5,419
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.
 
  • #4
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
 
  • #5
33,722
5,419
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.
 
  • #6
254
8
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.
 
  • #7
33,722
5,419
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.
 
  • Like
Likes jim mcnamara
  • #8
254
8
When I think about it that way, the '&&' does make sense. Thank you very much!
 

Related Threads on What is wrong with this WHILE loop?

Replies
1
Views
3K
Replies
3
Views
7K
Replies
1
Views
17K
Replies
1
Views
2K
Replies
12
Views
5K
Replies
1
Views
3K
Replies
12
Views
2K
  • Last Post
Replies
4
Views
24K
  • Last Post
Replies
7
Views
2K
Top