else - Execute statements if condition is false

Syntax

if expression, statements1, else statements2, end

Description

if expression, statements1, else statements2, end evaluates expression and, if the evaluation yields logical 1 (true) or a nonzero result, executes one or more MATLAB commands denoted here as statements1 or, if the evaluation yields logical 0 (false), executes the commands in statements2. else is used to delineate the alternate block of statements..

A true expression has either a logical 1 (true) or nonzero value. For nonscalar expressions, (for example, "if (matrix A is less than matrix B)"), true means that every element of the resulting matrix has a true or nonzero value.

Expressions usually involve relational operations such as (count < limit) or isreal(A). Simple expressions can be combined by logical operators (&,|,~) into compound expressions such as (count < limit) & ((height - offset) >= 0).

See Program Control Statements in the MATLAB Programming Fundamentals documentation for more information on controlling the flow of your program code.

Examples

In this example, if both of the conditions are not satisfied, then the student fails the course.

if ((attendance >= 0.90) & (grade_average >= 60))
   pass = 1;
else
   fail = 1;
end;

See Also

if, elseif, end, for, while, switch, break, return, relational operators, logical operators (elementwise and short-circuit)

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS