MATLAB - The if...else...end Statement
An if statement can be followed by an optional else statement, which executes when the expression is false.
The syntax of an if...else statement in MATLAB is:
if <expression> % statement(s) will execute if the boolean expression is true <statement(s)> else <statement(s)> % statement(s) will execute if the boolean expression is false end
If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.
Create a script file and type the following code:
a = 100; % check the boolean condition if a < 20 % if condition is true then print the following fprintf('a is less than 20\n' ); else % if condition is false then print the following fprintf('a is not less than 20\n' ); end fprintf('value of a is : %d\n', a);
When the above code is compiled and executed, it produces the following result:
a is not less than 20 value of a is : 100