While loop with multiple conditions
89 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(3 个)
dpb
2015-12-25
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 个评论
Image Analyst
2015-12-26
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.
Kamuran
2015-12-25
1 个评论
dpb
2015-12-25
"... (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.
Image Analyst
2015-12-25
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 个评论
Image Analyst
2015-12-26
编辑:Image Analyst
2015-12-26
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
2015-12-26
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!