English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
An optional else statement can follow the if statement, which is executed when the expression is false.
The syntax of if ... else statements in MATLAB is-
if <expression> %If the boolean expression is true, execute the following statement <statement(s)> else <statement(s)> %If the boolean expression is false, execute the following statement end
If the calculated result of the boolean expression is true, the if code block will be executed, otherwise the else code block will be executed.
Create a script file and enter the following code-
a = 100; %Check Boolean Condition if a < 20 %If condition is true, print the following content fprintf('a is less than 20\n'); else %If condition is false, print the following content fprintf('a is not less than 20\n'); end fprintf('The value of a is: %d\n', a);After compiling and executing the above code, the following result will be produced-
a is not less than 20 The value of a is: 100