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).
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.
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