Use of "Break" in switch statement

조회 수: 114 (최근 30일)
Nicholai
Nicholai 2011년 4월 18일
편집: Walter Roberson 2025년 2월 8일 19:01
I have a switch statement, and I would like to use a "break" to terminate some calculations if the switch statement is fulfilled.
But which statement can I use, which is similar to "break" like used for loops? Cause I have so far just used "exit" but that statement closes Matlab and I only want to abort some calculations but still not close Matlab completely....
Many Thanks!
  댓글 수: 2
Sole gak
Sole gak 2019년 1월 30일
편집: Sole gak 2019년 1월 30일
you can use return
%% in case you have a simple switch case
a = 1; b = 1;
switch a
case 1
disp('foo');
if (b ==1)
disp('bar');
return;
end
disp('bidon');
end
%% this works as well if you have your switch case in a loop
a = 1; b = 1;
while (true)
switch a
case 1
disp('foo');
if (b ==1)
disp('bar');
return;
end
disp('foobar');
end
end
Hope that helps,
S'
Jose
Jose 2021년 7월 13일
편집: Jose 2021년 7월 13일
Commands "break", "continue" and "return" work all very nicely within a "switch / case":
function switch_break
if fn2
disp('function fn2 went till the end');
else
disp('function fn2 aborted')
end
% end of switch_break function
function ret= fn2
ret= 0; % return zero if not done all runs
for i= 1:5
fprintf('i=%d of 5: ', i);
[~,~, button]= ginput(1);
switch button
case 1
disp('button 1, continue to the next case');
continue
case 2
disp('button 2, break the loop');
break
case 3
disp('button 3, abandon function');
return
case 27
disp('ESC key, continue after the switch')
end
disp(' more work done after the switch')
end
disp(' more work done after the loop')
ret= 1; % went till the end

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

답변 (3개)

Anne van Rossum
Anne van Rossum 2014년 8월 13일
The following is a "trick" that will break out of the switch.
for br=1:1
switch val
case 'one'
...
if ~some_condition
...
break;
end
case 'two'
...
case 'three'
...
otherwise
...
end
end
As you can see, there is an additional for-loop that is run only once with the only purpose to provide a way to break out of the switch statement.
I think the absence of a break in a switch statement is because people thought the only reason for it was to provide fall-through in early C. Personally, I think people should not tell you how to structure your code, so denying someone to break where he/she wants is a bit presumptuous.
  댓글 수: 1
Axel Enrique Chávez Barajas
Axel Enrique Chávez Barajas 2025년 2월 8일 18:28
Wow! 11 years later and I found this trick really useful. Thank you

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


Walter Roberson
Walter Roberson 2011년 4월 18일
break will terminate the enclosing loop.

Jiro Doke
Jiro Doke 2011년 4월 18일
What do you mean by "if the switch statement is fulfilled"? Are you trying to get out of the whole switch-case block while you are inside one of the case statements? I would just use if-else-end in your case statement:
switch val
case 'one'
...
if ~some_condition
...
end
case 'two'
...
case 'three'
...
otherwise
...
end
If "some_condition" is satisfied, it would get out of the "one" case. Is this what you mean?
Also, in case you're thinking that it works like C, as you can see from the documentation for switch (see Tips), MATLAB switch does not fall through, so you don't need a "break" or "return" after each case statement.
  댓글 수: 2
Nicholai
Nicholai 2011년 4월 19일
편집: Walter Roberson 2025년 2월 8일 19:01
I have this line of code:
function closeBar(src,evnt)
|selection = questdlg('Do you want to stop this process?',...
'Stop process',...
'Yes','No','Yes');
switch selection,
case 'Yes',
exit
case 'No'
return
end
It's if case "Yes" is fulfilled I need some statement which stops all the ongoing processes in all the m-files in my work space...
An "exit" function does this but it also closes the entire Matlab application. I only need it to stop the calculations not close the program...
Thank you for your reply!
Walter Roberson
Walter Roberson 2011년 4월 19일
Use a call to error()

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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by