Skip to Main Content Skip to Search
Product Documentation

else - Optional keyword within an if statement

Syntax

if expression
   statements
elseif expression
   statements
else
   statements
end

Description

An if statement evaluates an expression, and executes a group of statements when the expression is true.

elseif and else are optional, and execute only when previous expressions in the if statement are false.

For more information about valid expressions and when they are true, see the if reference page.

Examples

Find the indices of values in a vector that are greater than a specified limit:

A = rand(1,10);
limit = .75;

B = (A > limit);   % B is a vector of logical values
if any(B)
  fprintf('Indices of values > %4.2f: \n', limit);
  disp(find(B))
else
  disp('All values are below the limit.')
end

See Also

end | for | if | switch | while

  


Free MATLAB Interactive Kit

Explore how to use MATLAB to make advancements in engineering and science.


Download free kit

Trials Available

Try the latest version of MATLAB and other MathWorks products.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS