Matlab Programming Control Structures
Matlab Programming Control Structures
Matlab is a programming language with the same basic control structures that are available in every other programming language. This document shows examples of these structures in action. Contents if-else while for if-else example 1
a = 2; b = 76; if (a < 1 || (a > 1 && b < 80)) disp('Condition is true') else disp('Condition is false'); end Condition is true
Example 2
a = 83; b = 89; if (a < 50) disp('a elseif (b > disp('b else disp('a end
is less than 50') 1000) is greater than 1000 and a is not less than 50'); is not less than 50 and b is not greater than 1000');
while Example 1
k = 0; while (k < 4) disp(k) disp('Looping'); k = k + 1; end 0 Looping 1 Looping 2 Looping 3
Looping
for
x = [-9 4 2 4 56 6]; for k = 1 : 4 disp(x(k)) end -9 4 2 4 x = [-9 4 2 4 56 6]; for k = 1 : length(x) disp(x(k)) end -9 4 2 4 56 6 for k = 1: 2 : length(x) disp(x(k)) end -9 2 56