> Financial Mathematics |
If/else conditions
Basic decision making in Matlab using If/else conditions.
The following Matlab program will ask user to insert a number. If the number is greater than 10, it displays "The number is greater than 10", and does nothing otherwise.
IMPORTANT: Write this program in 'Script File Editor' and save it giving a name. Then write that name in the "Command Window" to run the program. Suppose we save this program with name "myprogram".
In Script File Editor
if (a > 10)
disp ('The number is greater than 10');
end
In Command Window
Similarly, write all the following programs in "Script File Editor". Save with a name. Write that name in "Command Window" and press Enter.
The following Matlab program will ask user to insert a number. If the number is greater than 10 it displays "The number is greater than 10". Otherwise it displays "The number is 10 or less than 10".
if (a > 10)
disp ('The number is greater than 10');
else
disp ('The number is 10 or less than 10');
end
The following Matlab program will ask user to insert a number. If the number is greater than 10 it displays "The number is greater than 10". If the number is 10, it displays "The number is 10". And, if the number is less than 10, it displays "The number is less than 10".
if (a > 10)
disp ('The number is greater than 10');
elseif (a == 10)
disp ('The number is 10')
else
disp ('The number is less than 10');
end