Looping and Conditional-Control Flow : Absolute value | switch. .. case ... otherwise ... end

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

Looping and Conditional-Control Flow : Conditional/Branches

Take a look also at the previous article: Looping and Conditional-Control Flow : Iteration conditioned

Control Flow # 2: Conditional / Branches
Conditional is a useful control to turn the program into a specific process. Usually used to complete the program that has many processes, but in one occasion execution only run one or more selection process based on certain conditions

Conditional relative value (if. .. elseif ... else ... end)
This conditional syntax can be used for conditions that are in a certain interval value or absolute, either numeric or string. So that control flow is the most common type used by the programmer. The way the writing is as follows:

if condition
  command
elseif
  command
else
  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 6th Training Program');
disp('--------------------------');

test1 = input ( 'value test1 =');
test2 = input ( 'value utest2 =');
test3 = input ( 'value test3 =');

na = (test1 * 20/100) + (test2 * 30/100) + (test3 * 50/100);

disp ( '[final value =' num2str (na)]);

if na> 80
   disp ( 'Your grade = A');
elseif na <= 80 & na> 70
   disp ( 'Your grade = B');
elseif na <= 70 & na> 60
   disp ( 'Your grade = C');
elseif na <= 60 & na> 50
   disp ( 'grade you = D');
else
   disp ( 'Your grade = E');
end;

3. When finished typing the above programs, you save in the directory c: / mytraining, with the name

training06.m

4. Ensure your file storage directory is contained in the directory search list MATLAB. Learn it in the MATLAB directory management here.Then type the file name without the extension training06:
>> training06

5. Press Enter, then the program will run and produce as follows:
 --------------------------
My 6th Training Program
--------------------------
test1 value = 60
test2 value = 80
test3 value = 50
Your grade = C
>>

6. Done

Looping and Conditional-Control Flow : Iteration conditioned

Take a look also at the previous article: Looping and Conditional-Control Flow : Repetition / Iteration / Looping 

Iteration conditioned (While. .. end).
This syntax is used to perform the process without a known repetition of repetition. Iterations of this type of repetition only stop when it reaches certain conditions. Eg calculate the amount of glass needed to accommodate the contents of the tub. The way the writing is as
follows:

while conditions
commands
end

For clarity, the following are examples of its use in the program:
1. In the command window, type:
>> Edit

2. Press Enter, then when MATLAB editor appear, type or copy the program below:
clear all;
clc;

disp ('--------------------------');
disp ( 'My 5th Training Program');
disp ('--------------------------');

vbasin = input ( 'volume of the tub (ltr) =');
vglass = input ( 'glass volume (ltr) =');

nglass = 0;while vbasin> 0nglass = nglass +1;
vbasin = vbasin - vglass;
end;

disp ([ 'glasses required =' num2str (nglass)]);

3. When finished typing the above programs, you save in the directory c: / mytraining, with the name training05.m

4. Ensure your file storage directory is contained in the directory search list MATLAB. Learn it in the MATLAB directory management  here. Then type the file name without the extension training04:
>> training05

5. Press Enter, then the program will run and produce as follows:
--------------------------
My 5th Training Program
--------------------------
bath volume (ltr) = 2
glass volume (ltr) = 0.2
glass required = 11

6. Done