The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: While loop with two conditions?
Replies: 4   Last Post: Nov 21, 2012 9:15 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Nasser Abbasi

Posts: 6,677
Registered: 2/7/05
Re: While loop with two conditions?
Posted: Nov 21, 2012 9:06 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On 11/21/2012 7:52 AM, Sensible Man wrote:
> "Nasser M. Abbasi" <> wrote in message <k8ikrc$do$>...
>> On 11/21/2012 7:12 AM, Sensible Man wrote:
>>> I'm sure this is a very simple question, but is it possible to create a while
>>> loop with two initial conditions?
>>> I basically need
>>> while x<3, or y>500,
>>> execute loop
>>> end if either x<3 or y>500 is met


>> for scalars, use ||
>> ------------------
>> x=2; y=603;
>> while x<3 || y>600
>> x=x+1
>> y=y-1
>> end
>> ------------------


> Awesome thanks. However, when I run the code you provided above, the while
>loop doesn't terminate when x>3, it only seems to stop when y>600?

It will run as long as the condition is TRUE. The condition
is TRUE if x<3 OR y>600. So if one condition is false, it
is ok, it will still run the loop, as long as the other is true
since the test is an OR

It will stop the loop when both checks are FALSE.

That is what I thought you wanted. But if you want to
stop when one of the 2 conditions condition fail,
change it to

x=2; y=603;

while x<3 && y>600


Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.