What is wrong with this WHILE loop?

  • Context: MATLAB 
  • Thread starter Thread starter ecastro
  • Start date Start date
  • Tags Tags
    Loop Matlab
Click For Summary

Discussion Overview

The discussion revolves around a programming issue related to the behavior of a WHILE loop in a code snippet. Participants explore the conditions under which the loop should terminate, focusing on logical operators and their implications in the context of the loop's execution. The conversation includes technical reasoning about the use of OR and AND conditions in the loop's header.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a WHILE loop that should stop when 'a' reaches 3, but it does not, raising a question about the logic used in the loop's condition.
  • Another participant questions the understanding of logical expressions, specifically the outcome of (1 OR 0).
  • Several participants discuss the implications of using OR (||) versus AND (&&) in the loop's condition, with some suggesting that the original logic may lead to an infinite loop due to the condition involving 'b'.
  • One participant proposes a modified loop condition using AND (&&) instead of OR (||), suggesting it would resolve the issue, while another participant emphasizes the importance of understanding De Morgan's Laws in this context.
  • There is a discussion about the clarity of the original poster's explanation regarding the value of 'b' and how it affects the loop's execution.
  • Some participants express uncertainty about the conditions needed to run or quit the loop, leading to further clarification attempts regarding the logical structure of the conditions.

Areas of Agreement / Disagreement

Participants express differing views on the correct logical structure for the loop's condition. While some suggest that changing from OR to AND resolves the issue, others argue that the original logic is misunderstood. The discussion remains unresolved regarding the optimal condition for the loop.

Contextual Notes

Participants note that the original poster's information may be incomplete, leading to confusion about the intended logic of the loop. There are also references to potential infinite loops based on the conditions set in the WHILE statement.

ecastro
Messages
249
Reaction score
8
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.
 
Physics news on Phys.org
What does the expression ( 1 OR 0 ) give you?
 
  • Like
Likes   Reactions: mfb
ecastro said:
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;
endb = 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
 
Image Analyst said:
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;
endb = 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.
 
Mark44 said:
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.

Mark44 said:
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 said:
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.

Mark44 said:
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   Reactions: jim mcnamara
When I think about it that way, the '&&' does make sense. Thank you very much!
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K