- Matlab Tutorial
- MATLAB - Home
- MATLAB - Overview
- MATLAB - Environment Setup
- MATLAB - Syntax
- MATLAB - Variables
- MATLAB - Commands
- MATLAB - M-Files
- MATLAB - Data Types
- MATLAB - Operators
- MATLAB - Decisions
- MATLAB - Loops
- MATLAB - Vectors
- MATLAB - Matrix
- MATLAB - Arrays
- MATLAB - Colon Notation
- MATLAB - Numbers
- MATLAB - Strings
- MATLAB - Functions
- MATLAB - Data Import
- MATLAB - Data Output
- MATLAB Advanced
- MATLAB - Plotting
- MATLAB - Graphics
- MATLAB - Algebra
- MATLAB - Calculus
- MATLAB - Differential
- MATLAB - Integration
- MATLAB - Polynomials
- MATLAB - Transforms
- MATLAB - GNU Octave
- MATLAB - Simulink
- MATLAB Useful Resources
- MATLAB - Quick Guide
- MATLAB - Useful Resources
- MATLAB - Discussion
MATLAB - The Nested if Statements
It is always legal in MATLAB to nest if-else statements which means you can use one if or elseif statement inside another if or elseif statement(s).
Syntax
The syntax for a nested if statement is as follows −
if <expression 1> % Executes when the boolean expression 1 is true if <expression 2> % Executes when the boolean expression 2 is true end end
You can nest elseif...else in the similar way as you have nested if statement.
Example
Create a script file and type the following code in it −
a = 100; b = 200; % check the boolean condition if( a == 100 ) % if condition is true then check the following if( b == 200 ) % if condition is true then print the following fprintf('Value of a is 100 and b is 200\n' ); end end fprintf('Exact value of a is : %d\n', a ); fprintf('Exact value of b is : %d\n', b );
When you run the file, it displays −
Value of a is 100 and b is 200 Exact value of a is : 100 Exact value of b is : 200
matlab_decisions.htm
Advertisements