What is wrong with this WHILE loop?

  • MATLAB
  • Thread starter ecastro
  • Start date
  • Tags
    Loop Matlab
In summary: The "&&" operator evaluates the two conditions as "true" if both are true, while the "||" operator evaluates the two conditions as "true" only if one of the conditions is true.
  • #1
ecastro
254
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
  • #2
What does the expression ( 1 OR 0 ) give you?
 
  • Like
Likes mfb
  • #3
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.
 
  • #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;
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
 
  • #5
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.
 
  • #6
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.
 
  • #7
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 jim mcnamara
  • #8
When I think about it that way, the '&&' does make sense. Thank you very much!
 

Related to What is wrong with this WHILE loop?

1. What is the purpose of a WHILE loop?

A WHILE loop is used in programming to repeatedly execute a block of code as long as a specified condition is met. This allows for efficient and automated execution of tasks.

2. What are some common errors in WHILE loops?

Some common errors in WHILE loops include: forgetting to update the loop condition, causing an infinite loop; forgetting to initialize variables before the loop; and using incorrect syntax for the loop condition.

3. How can I fix a WHILE loop that is not working?

To fix a WHILE loop that is not working, first check the loop condition to ensure it is correct and that any variables used in the condition are being updated within the loop. Also, make sure that the loop is not running infinitely. Additionally, check for any syntax errors in the code and make necessary corrections.

4. Can a WHILE loop be nested within another loop?

Yes, a WHILE loop can be nested within another loop, such as a FOR loop or another WHILE loop. This allows for more complex logic and allows for finer control over the execution of code.

5. Are there any alternatives to using a WHILE loop?

Yes, there are other loop structures such as FOR and DO-WHILE loops that can also be used to achieve similar results. Additionally, some programming languages may have built-in functions or methods that can be used to iterate through a collection of data without the need for a loop.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
558
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
593
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
Back
Top