Program should continue running the remaining script

조회 수: 2 (최근 30일)
Fazal Muhammad Ali Khan
Fazal Muhammad Ali Khan 2020년 12월 21일
댓글: Fazal Muhammad Ali Khan 2020년 12월 21일
I am running a script with multiple for loops. In each loop when iteration reaches a particular condition I have to stop it else dimensions reaches out of bound . For that I am using return statement (If loop when the condition meets). The program runs fine but when it reaches return statement , it stops further execution of script.So I have to evalute the remaining scripts (for loops) manually. Is there any method that remaining script (other for loops independent of this for loop ) also gets executed? Thanks in advance

채택된 답변

Ive J
Ive J 2020년 12월 21일
return is quite intuitive and it does what it's suppoed to do: Returns control to invoking script or function. If you wanna continue other loops, you should use break
for i = 1:100
if i > 10
fprintf('I''m done with first loop at counter: %d\n', i)
break
end
end
for j = 1:50
if j > 30
fprintf('I''m also done with second loop at counter: %d\n', j)
break
end
end
fprintf('All done!\n')
  댓글 수: 3
Fazal Muhammad Ali Khan
Fazal Muhammad Ali Khan 2020년 12월 21일
@Ive Ive : Thank you very much. It now works perfectly fine. Keep helping :) Thanks once again
Fazal Muhammad Ali Khan
Fazal Muhammad Ali Khan 2020년 12월 21일
@Ameer Hamza: I did comment but it was placed under flag banner. Have now commented where it should have been. Thanks for pointing out.

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

추가 답변 (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