| MATLAB Function Reference | Search  Help Desk |
| end | Examples See Also |
Terminate for, while, switch, try, and if statements or indicate last index
while expression % (or if, for, or try)
statements
end
B = A(index:end,index)
end is used to terminate for, while, switch, try, and if statements.
Without an end statement, for, while, switch, try, and if wait for further
input. Each end is paired with the closest previous unpaired for, while,
switch, try, or if and serves to delimit its scope.
end command also serves as the last index in an indexing expression. In
that context, end = (size(x,k)) when used as part of the kth index.
Examples of this use are X(3:end) and X(1,1:2:end-1). When using end to
grow an array, as in X(end+1)=5, make sure X exists first.
end used with for and if. Indentation provides easier readability.
for i = 1:n
if a(i) == 0
a(i) = a(i) + 2;
end
end
Here, end is used in an indexing expression:
A = rand(5,4) B = A(end,2:end)In this example,
B is a 1-by-3 vector equal to [A(5,2) A(5,3) A(5,4)].
break Terminate execution of for or while loop
for Repeat statements a specific number of times
if Conditionally execute statements
return Return to the invoking function
switch Switch among several cases based on expression
try Begin try block
while Repeat statements an indefinite number of times