While loop with multiple conditions

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)

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

Because when I before I start the loop Nx=1000 (pre-set). So this will stop when Nx<5000 that means it won't enter the loop. I want the loop try to run and increase the resolution_check and decrease the mX_check to those levels as long as Nx is less than 5000.
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
>>
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

"... (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.

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

Let me tell you what happens during the loop. Nx increases, resolution increases and mX decreases.
I want to while loop stop executing when resolution_check >= 8 (that is good enough resolution for me) but at the same time mX_check should be less than 0.1. Both these two should met. Meanwhile the third variable Nx has to be less than 5000.
So if the resolution condition + mX condition met before Nx reaches 5000 => break
if Nx reaches 5000 loop breaks no matter what resolution or mX are.
So my understanding command "while"
  • while resolution_check less than 8 continue to run
  • while mX_check larger than 0.1 continue run
  • while Nx less than 5000 continue to run
Loop Stops
  • When resolution_check is 8 or bigger + mX_check less than 0.1
  • or when Nx is bigger than 5000
while (resolution_check<8 mX_check>0.5 ) && Nx<5000 this looks like it is working
dpb
dpb on 26 Dec 2015
Edited: dpb on 26 Dec 2015
That CAN'T be working, it's illegal syntax --
>> while (resolution_check<8 mX_check>0.5 ) && Nx<5000
disp(Nx)
Nx=Nx+1000;
end
while (resolution_check<8 mX_check>0.5 ) && Nx<5000
|
Error: Unexpected MATLAB expression.
>>
"...stop executing when resolution_check >= 8 but at the same time mX_check should be less than 0.1"
AHA!! That's a different condition than you'd outlined before (and, admittedly, I skimmed over it earlier). As IA notes, then you need a compound expression which apparently is where you're having syntax issues. For a while to continue the condition has to be true and so
while (resolution_check<8 | mX_check>0.1) & Nx<5000
...
will make either convergence parameter being out of range while the number of iterations is under the limit cause the loop to continue. It might be easier to see if it were rewritten a little differently as
while Nx<5000
...
% computations here
...
if resolution_check>=8 & mX_check<=0.1), break, end
end
Here the check is for the joint conditions of convergence being satisfied ("AND") and if satisfied the loop on the total number of iterations is exited. But, the while is the same logic just in one statement and as the continuing condition instead of the terminating one.
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?
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 Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 25 Dec 2015

Commented:

on 26 Dec 2015

Community Treasure Hunt

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

Start Hunting!