An equivalent to jump/goto ?

조회 수: 82 (최근 30일)
Olivier
Olivier 2012년 10월 26일
댓글: Walter Roberson 2018년 1월 7일
Hello all,
I would like to jump between my loops whole/for. I know that I can use the command break, but it doesn't work in this situation.
There is the architecture of my code :
while (condition1)
Action1
while (Condition2)
Action2
for i=1:N
if (Condition3)
Action3
%Goto second While loop
elseif (Condition4)
Action4
%Goto first While loop
end
end
end
end
Is it possible to do these 'Goto' with Matlab ? or an equivalent ?
Thanks.
  댓글 수: 3
Walter Roberson
Walter Roberson 2018년 1월 7일
Mauro Alejandro Pereira comments to Loginatorist:
This is not a solution.
Walter Roberson
Walter Roberson 2018년 1월 7일
Mauro Alejandro Pereira:
Notice that Loginatorist's remark was posted as a Comment, not as an Answer. Individual Comments are for discussion, and need not be solutions.

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

채택된 답변

Sean de Wolski
Sean de Wolski 2012년 10월 26일
The MATLAB equivalent is to use functions that you call when you want to go somewhere else.
This is explained in the documentation for function, available here:

추가 답변 (3개)

Jan
Jan 2012년 10월 26일
편집: Jan 2012년 10월 26일
Only a small change solves the problem:
while (condition1)
Action1
Proceed = true;
while (Condition2 && Proceed)
Action2
for i=1:N
if (Condition3)
Action3
%Goto second While loop
break; % <==
elseif (Condition4)
Action4
%Goto first While loop
Proceed = false; % <==
break; % <==
end
end
end
end
This is very similar to Matt Fig's solution, only the test of his bf is embedded in the condition of the WHILE.
  댓글 수: 1
Matt Fig
Matt Fig 2012년 10월 26일
편집: Matt Fig 2012년 10월 26일
Yep. I use this similar construct too. Which one I choose depends on what I am doing. When translating old FORTRAN code, or when I am working with a FORTRAN programmer, (both occur frequently in my line of work) I am more likely to use my approach. When writing from scratch I am likely to use the one Jan shows.

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


Matt Fig
Matt Fig 2012년 10월 26일
편집: Matt Fig 2012년 10월 26일
There has been a feature request for quite some time to allow BREAK to have a level passed to it. Perhaps add your name to the chorus of voices requesting such a feature. Until then, use this kind of thing....
cnt1 = 1;
cnt2 = 1;
while cnt1<10
disp('In first loop')
bf = 0; % Break flag for throwing back to first loop
cnt1 = cnt1 + 1;
while cnt2<10
disp('In second loop')
cnt2 = cnt2 + 1;
for i=1:20
if cnt2<5
cnt2 = cnt2 + 1;
disp('breaking back to 2nd loop')
break
%Goto second While loop
elseif cnt1<5
disp('breaking back to 1st loop')
cnt1 = cnt1 + 1;
bf = 1; % set flag to go to first loop
break
end
end
if bf % break out of second loop when set
break
end
end
end

Walter Roberson
Walter Roberson 2012년 10월 26일
  댓글 수: 2
Jan
Jan 2012년 10월 27일
As usual I cannot hesitate to publish my hopes, that Matlab will get a COMEFROM in the future also.
Walter Roberson
Walter Roberson 2012년 10월 27일
As long as it isn't ALTER! Single worst programming feature I've ever seen!

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

카테고리

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