Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Help if statment inside a while loop

조회 수: 5 (최근 30일)
Liam Ryan
Liam Ryan 2019년 10월 13일
마감: MATLAB Answer Bot 2021년 8월 20일
Hi I am trying to implement an if statment inside a while loop. So I have something of this kind:
error = 999
b = 10
a = 0;
c = 0;
while error> 10
b = b+10
error = error - b
if (error >= 500)
a = a+9
elseif error>=200
c = c+9
end
end
I just want the if statment and elseif statment to execute once, and not execute again EVEN IF the if and else if conditions are true. I tried writing out the return, continue and break words but it doesnt work.
So after the while statment has met its condition and the error becomes less then 10, I want the vairable a =9 and c = 9 at the end.
Please someone help out!!
thank you

답변 (1개)

darova
darova 2019년 10월 13일
Try to add these conditions
if (error >= 500) && a<9
a = a+9
elseif error>=200 && c<9
c = c+9
end
Please use this button for code inserting
CODE_INSER.PNG
  댓글 수: 3
darova
darova 2019년 10월 13일
Of course! Here is another way:
error = 999
b = 10
a = 9;
c = 9;
while error> 10
b = b+10
error = error - b
end
Liam Ryan
Liam Ryan 2019년 10월 13일
THANKS!!

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by