conditional statements and while loop

Hi,
I have the following while loop
while (dif1 > tol) && (dif2 > tol) && (dif3 > tol)
procedure
end
I want all the conditions to be simultaneously satisfied. However, when I execute the code, the second condition dif2 > tol is not satisfied although the algorithm stops.
what I am doing wrong here ?

3 个评论

If the second condition, dif2>tol is not satisfied then the while loop will stop because the code is "saying" do this procedure so long as dif1>tol AND dif2>tol AND dif3>top. So as soon as one of the three is not satisfied, then we stop executing the procedure. What are you expecting to happen instead of this?
msh
msh 2014-11-24
编辑:msh 2014-11-24
I see. Then this is not what I want. I need ALL conditions to be satisfied in the same time. That is, I need the loop to go on, until ALL three are false. How I should modify the conditions then?
The double operands ‘&&’ and ‘| | ‘short circuit’ the comparisons. See: Logical Operators: Short-Circuit && | |. If the first is false, it will not evaluate any of the others.

请先登录,再进行评论。

 采纳的回答

while (dif1 > tol) || (dif2 > tol) || (dif3 > tol)
should work if you want to carry on until all are false.

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

提问:

msh
2014-11-24

评论:

msh
2014-11-24

Community Treasure Hunt

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

Start Hunting!

Translated by