Control Flow # 1: Repetition / Iteration / Looping
Repetition is the type of controller that allows you to streamline the writing script programs, especially those for programs that require the repeatedly. Repetition is often also referred to as iteration or looping.
Henceforth we only use the term iteration.
Limited iteration (for. .. end)
This iteration syntax is used to perform a repetition of the process has known quantity. For example to calculate the factorial 5, it clearly known number of iterations is 5. The way the writing is as follows:
For variable = start: interval: finish
commands
end
For more details, here is an example of its use in the program as follows:
1. In the command window, type:
>> Edit
2. Press Enter, then came MATLAB Editor and you type the program under follows:
clear all;
clc;
Disp ('--------------------------');
Disp ( 'My 4th Training Program');
Disp ('--------------------------');
data = input ( 'limit of iterations =');
for n = 1: data
for m = data: -1:1
x (n, m) = n ^ 2 + (5 * m)
end;
end;
3. When finished typing the above programs, you save in a directory 'c: / mytraining', with the name training04.m
4. Ensure your file storage directory is contained in the list MATLAB directory search. Learn directory management here. Then type the file name without the extension training04:
>> training04
5. Press Enter, then the program will run and produce as follows:
--------------------------
My 4th Training Program
--------------------------
iteration limit = 1
x =
6 11 16
9 14 19
14 19 24
6. Done
In the next article we'll still talk about another form of iteration but it is for unknown quantity.
0 comments:
Post a Comment