Absolute value of the conditional (switch. .. case ... otherwise ... end)
This conditional syntax can only be used for conditions with a value that is not within certain intervals, can be either numeric or string. Command syntax-case switches are as follows:
switch variable
case value1
command
case value2
command
.
.
.
otherwise
command
end
For clarity, the following are examples of its use in the program:
1. In the command window, type:
>> edit
2. Press Enter, then came MATLAB Editor and you type the program under the following:
clear all;
clc
disp ('--------------------------');
disp ( 'My 7th Training Program');
disp ('--------------------------');
disp ( 'options calculation formula');
disp ('1. cube surface area');
disp ('2. Volume of the cube ');
disp ('3. Ball surface area');
disp ('4. Volume of the Ball ');
disp ( '');
select = input ( 'Your choice (1-4) ->');
switches select
case 1
disp ( 'Calculate the area of the cube');
disp ('------------------');
length = input ( 'length = box');
area = length ^ 6 * 2;
disp ([ 'cube surface area =' num2str (area)]);
case 2
disp ( 'Calculate the Volume cube');
disp ('------------------');
length = input ( 'length = box');
volume = length ^ 3;
disp ([ 'volume of the cube =' num2str (volume)]);
case 3
disp ( 'Calculate the area of the ball');
disp ('------------------');
radius = input ( 'radius of ball =');
area = 4 * pi * radius ^ 2;
disp ([ 'ball surface area =' num2str (area)]);
case 4
disp ( 'Calculate the volume of the ball');
disp ('------------------');
radius = input ( 'radius of ball =');
volume = 4 * pi * radius ^ 3 / 3;
disp ([ 'Volume of the ball =' num2str (volume)]);
otherwise
disp ( 'Your choice inconsequential !!!');
end;
3. When finished typing the above programs, you save in the directory c: / mytraining, with the name training07.m
4. Ensure your file storage directory is contained in the directory search list MATLAB. Then type the file name without the extension training06:
>> training07
5. Press Enter, then the program will run and produce as follows:
--------------------------
My 7th Training Program
--------------------------
Calculation formula options
1. cube surface area
2. Volume of the cube
3. Ball surface area
4. Volume of the Ball
Your choice (1-4) -> 2
Calculate the volume cube
------------------
long box = 3
volume of the cube = 27
6. Done