필터 지우기
필터 지우기

conditional statements and while loop

조회 수: 2 (최근 30일)
msh
msh 2014년 11월 24일
댓글: msh 2014년 11월 24일
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
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?
Star Strider
Star Strider 2014년 11월 24일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Adam
Adam 2014년 11월 24일
while (dif1 > tol) || (dif2 > tol) || (dif3 > tol)
should work if you want to carry on until all are false.
  댓글 수: 1
msh
msh 2014년 11월 24일
yes, I already did it ! thanks

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by