While loop with multiple conditions

52 views (last 30 days)
Kamuran
Kamuran on 25 Dec 2015
Commented: Image Analyst on 26 Dec 2015
Hello, I am trying to set a while loop but I am having hard time to make it work the way I wanted to work.
I want the loop continue running as long as Nx less than 5000 while trying to reach resolution_check<8 and mX_check>0.1
while ((resolution_check<8) && (mX_check>0.1)) || (Nx<5000);
I can't bound the Nx less than 5000 with this and loop stops either mX_check or resolution_check reaches the condition.
Any ideas why I am having a problem?

Answers (3)

dpb
dpb on 25 Dec 2015
while (resolution_check<8) & (mX_check>0.1) & (Nx<5000)
....
Not sure why you left the second conditional off but that should do it...
  3 Comments
dpb
dpb on 25 Dec 2015
Edited: dpb on 26 Dec 2015
No, 1000<5000 so
>> resolution_check=0; mX_check=1; Nx=1000;
>> while (resolution_check<8) & (mX_check>0.1) & (Nx<5000)
disp(Nx)
Nx=Nx+2000;
end
1000
3000
>> Nx
Nx =
5000
>>
Image Analyst
Image Analyst on 26 Dec 2015
It will loop WHILE Nx<5000, which is why they call it a while loop. It will not stop when Nx<5000 as you said - that is incorrect. It WILL enter the loop and keep going until Nx>=5000 or one of the other conditions fails. Your whole understanding of how a while loop works is reversed - opposite to the way it actually does work so you'd better read up on it.

Sign in to comment.


Kamuran
Kamuran on 25 Dec 2015
Ok I wanted this
while (resolution_check<8 mX_check>1 ) && Nx<5000
but I think I am confused between (or) and && (and) . The way they work. For me one of the statement has to fail but it is not working like that. For me it is strange.
  1 Comment
dpb
dpb on 25 Dec 2015
"... (resolution_check<8 mX_check>1 ..." isn't legal syntax.
Again you've removed a conditional operator between the two logical operations.
'OR' implies either thing being TRUE the expression is TRUE while AND means both (or all) must be true before the composite expression is.

Sign in to comment.


Image Analyst
Image Analyst on 25 Dec 2015
This is ambiguous: "while trying to reach resolution_check<8 and mX_check>0.1" If resolution_check is 2, then that means you've reached (achieved) the condition of "resolution_check<8". So do you want to break out of the loop when resolution_check is 2? So let's just ask what conditions do you want to run the loop or break out of it:
resolution_check < 8 Run or break?
resolution_check >= 8 Run or Break?
mX_check > 0.1 Run or break?
mX_check <= 0.1 Run or break?
Please clarify.
If the loop "stops either mX_check or resolution_check reaches the condition." - well that's just not true. The loop will continue if the condition is met, and break if the condition(s) is not met. And you have && so if any one of those is not true, the loop will quit. So if resolution_check >= 8 or mX_check <= 0.1 then the condition is not true and it will break immediately. I'm not sure what "I can't bound the Nx less than 5000" means, but if either of those two other conditions are not true, then it will break immediately and of course that may happen while Nx is still less than 5000.
  4 Comments
Image Analyst
Image Analyst on 26 Dec 2015
Edited: Image Analyst on 26 Dec 2015
dpb is right - that is incorrect syntax. So we need to figure out if you mean
while (resolution_check<8 && mX_check>0.1) && Nx<5000
or
while (resolution_check<8 || mX_check>0.1) && Nx<5000
Exactly what does "resolution condition + mX condition met" mean? And what does " at the same time mX_check should be less than 0.1" mean? The sloppy terminology is preventing us from figuring out what to use, OR or AND. So mX_check should be less than 0.1, but what if it's not? What if it's 0.2 (meaning continue to run) but resolution_check is more than 8 (meaning to stop/break)? Do you continue to run, or do you break?
If you need Both resolution_check>=8 and mX_check<=0.1 to be true at the same time in order to break out of the loop, plus you want to break out regardless of those values if Nx ever gets more than 5000, you need to do it this way:
while (resolution_check < 8 || mX_check > 0.1) && Nx<5000
Now it will break if Nx ever meets or exceeds 5000, regardless of the values of resolution_check < 8 and mX_check. If Nx is less than 5000, the loop will continue if either resolution_check < 8 OR mX_check > 0.1, meaning that it will only break and it will break only if both resolution_check >= 8 AND mX_check <= 0.1. So does that do what you want?
Image Analyst
Image Analyst on 26 Dec 2015
Personally I'd do it like dpb showed last, with the "if" test inside the while loop and break out if it's true. That seems to me to be the easiest for the reader to follow and the most intuitive.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!