Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab What is wrong with this WHILE loop?

  1. Aug 26, 2016 #1
    Here is my code:

    Code (Text):
    while a ~= 3 || b <= 1;
    a = a - 1;
    (other expressions);
    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. jcsd
  3. Aug 26, 2016 #2
    What does the expression ( 1 OR 0 ) give you?
  4. Aug 26, 2016 #3


    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.
  5. Aug 26, 2016 #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 (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;

    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

  6. Aug 27, 2016 #5


    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);
    Note the change from "||" to "&&" in the while loop header.
  7. Aug 30, 2016 #6
    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.
  8. Aug 30, 2016 #7


    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.
  9. Aug 30, 2016 #8
    When I think about it that way, the '&&' does make sense. Thank you very much!
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted

Similar Threads - wrong WHILE loop Date
Mathematica While Loop in Mathematica Feb 25, 2018
Getting wrong answer using Maple (Physics package) Aug 4, 2015
X-Axis is wrong Sep 22, 2014
Mathematica plots wrong Jun 30, 2014
What is wrong in this Latex code? Jun 5, 2013