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

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;

disp('My 6th Training Program');

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');
   disp ( 'Your grade = E');

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


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


Post a Comment