| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
| On this page… |
|---|
Using break, continue, and return Multiple Conditions in a case Statement |
It's easy to confuse the break, continue, and return functions as they are similar in some ways. Make sure you use these functions appropriately.
Function | Where to Use It | Description |
|---|---|---|
for or while loops | Exits the loop in which it appears. In nested loops, control passes to the next outer loop. | |
for or while loops | Skips any remaining statements in the current loop. Control passes to next iteration of the same loop. | |
Anywhere | Immediately exits the function in which it appears. Control passes to the caller of the function. |
It is possible, but usually not advantageous, to implement switch-case statements using if-elseif instead. See pros and cons in the table.
switch-case Statements | if-elseif Statements |
|---|---|
Easier to read. | Can be difficult to read. |
Can compare strings of different lengths. | You need strcmp to compare strings of different lengths. |
Test for equality only. | Test for equality or inequality. |
A useful difference between switch-case statements in MATLAB and C is that you can specify string values in MATLAB case statements, which you cannot do in C.
switch(method)
case 'linear'
disp('Method is linear')
case 'cubic'
disp('Method is cubic')
endYou can test against more than one condition with switch. The first case below tests for either a linear or bilinear method by using a cell array in the case statement.
switch(method)
case {'linear', 'bilinear'}
disp('Method is linear or bilinear')
case (<and so on>)
end
In C, if you do not end each case with a break statement, code execution falls through to the following case. In MATLAB, case statements do not fall through; only one case may execute. Using break within a case statement is not only unnecessary, it is also invalid and generates a warning.
In this example, if result is 52, only the first disp statement executes, even though the second is also a valid match:
switch(result)
case 52
disp('result is 52')
case {52, 78}
disp('result is 52 or 78')
endSince MATLAB executes only one case of any switch statement, variables defined within one case are not known in the other cases of that switch statement. The same holds true for if-elseif statements.
In these examples, you get an error when choice equals 2, because x is undefined.
-- SWITCH-CASE -- -- IF-ELSEIF --
switch choice
case 1 if choice == 1
x = -pi:0.01:pi; x = -pi:0.01:pi;
case 2 elseif choice == 2
plot(x, sin(x)); plot(x, sin(x));
end endWhen you have statements in your code that could possibly generate unwanted results, put those statements into a try-catch block that will catch any errors and handle them appropriately.
The example below shows a try-catch block within a function that multiplies two matrices. If a statement in the try segment of the block fails, control passes to the catch segment. In this case, the catch statements check the error message that was issued (returned in MException object, err) and respond appropriately:
try
X = A * B
catch err
errmsg = err.message;
if(strfind(errmsg, 'Inner matrix dimensions'))
disp('** Wrong dimensions for matrix multiply')
endFor more information: See The try-catch Statement in the MATLAB Programming Fundamentals documentation.
You can also nest try-catch blocks, as shown here. You can use this to attempt to recover from an error caught in the first try section:
try
statement1 % Try to execute statement1
catch
try
statement2 % Attempt to recover from error
catch
disp 'Operation failed' % Handle the error
end
endTo force an early return from a function, place a return statement in the function at the point where you want to exit. For example,
if <done> return end
![]() | MATLAB Path | Save and Load | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |