English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
oneifstatement can be followed by one (or more) optionalelseif...and oneelsestatement, which is very useful for testing various conditions.
When using the if ... elseif ... else statement, remember the following points:
An if can have zero or another, and it must be after the other elseif.
An if can have zero or more elseif, and they must be located before else.
If the else if is successful, the rest of the elseif will not be tested.
if <expression 1> %When the expression1Execute when it is true <statement(s)> elseif <expression 2> %When the Boolean expression2Execute when it is true <statement(s)> Elseif <expression 3> %When the Boolean expression3Execute when it is true <statement(s)> else %Execute when all of the above conditions are not true <statement(s)> end
Create a script file and enter the following code-
a = 100; %Check Boolean Condition if a == 10 %If condition is true, then print the following content fprintf('Value of a is 10\n'); elseif( a == 20 ) %If the condition is true fprintf('Value of a is 20\n'); elseif a == 30 %If the condition is true fprintf('Value of a is 30\n'); else %If none of the conditions are true fprintf('None of the values are matching\n'); fprintf('Exact value of a is: %d\n', a); endAfter compiling and executing the above code, the following result will be produced-
None of the values are matching Exact value of a is: 100