Switching to further part of code from conditional statement 'if'
조회 수: 1 (최근 30일)
이전 댓글 표시
Suppose I have the following structure of code (below). The point is, in 2 different parts of my program I have exactly the same code, which is marked with the letter 'A'. Is there any way to avoid the repetition of the code A in conditional statement 'if' but instead, force the program to move to the further part of the program?
for n = 1:length(vectorOfNames)
% ...
try
% function that may return an error
catch
% error handling
if ~isempty(vectorOfNames2{n}) % if condition is True
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
else isempty(vectorOfNames2{n}) % if condition is False
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
continue
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
댓글 수: 0
채택된 답변
추가 답변 (2개)
Walter Roberson
2021년 2월 4일
No. Use functions to avoid repeating code.
Your example could be written as
try
something
catch
if isempty(whatever)
B
end
end
A
댓글 수: 0
Joanna Przeworska
2021년 2월 5일
댓글 수: 1
Walter Roberson
2021년 2월 5일
Acondition = true;
try
something
catch
if isempty(whatever)
B
continue
end
Acondition = C;
end
if Acondition
A
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!