========= FLOW CONTROL ============= ==================================== =========== conditions ============= ==================================== ---- something that eiter holds, or does not hold ---- values: true or false, stored in logical variables a = 3; b = (a>0); % b ... logical variable b == true % ans = 1 b == 1 % ans = 1 1 == true % ans = 1 1 == false % ans = 0 0 == false % ans = 1 0 == False % error: 'False' undefined ---- compound logical expressions --- (a>1) && (a<6) % a is in the interval (1,6) (a<=1) || (a>=6) % a is not in that interval ===== vectors of logical variables === a = [ 1 -7 3 2 -5 0]; b = (a>0) % b ... vector of logical variables b == true % element-wise b == false c = [ 2 -1 -5 2 0 -3]; a > c --- excercise: compute max([a; c]) without using max() m = c; d = find(a>c); m(d) = a(d); --- beware: different results for &, | and &&, || p = [ 1 0 1]; q = [ 0 1 1]; p & q % vector of logical variables p && q % one logical variable p | q p || q all(p) % an extension of AND (&&) any(p) % an extension of OR (||) ============================================= =========== conditional control ============= ============================================= -------- how the 'if' command works -- c = input('give a number: '); --- variant 1 ----- if c == 0 disp('the number is zero') end --- variant 2 ----- if c <= 0 disp('the number is not positive') else disp('the number is positive') end --- variant 3 ----- if c >= 10 disp('the number is greater than 9') elseif c > 0 disp('the number is positive and less than 10') elseif c == 0 disp('the number is zero') else disp('the number is negative') end ### Example 1 ---- check if the distance of two points exceeds a given length A = input('give the first point (for instance [ 1 5 7]): '); B = input('give the second point: '); dmax = input('give the limit: '); d = sqrt(sum((A-B).^2)); if d > dmax disp('the distance exceeds the limit'); else disp('the distance is within the limit'); end ----------------------------------------------------------- --- beware: 'if' command for vectors of logical variables b=[ 1 1 0 1]; % both b and ~b are false if b, c=2, else c=0 end % c=0 if ~b, c=2, else c=0 end % c=0 ~b % ans = 0 0 1 0 1 if b(1:2), c=2 else c=0 end % c=2 ============================================= ========== the 'for' loop =================== ============================================= -------- how the 'for' command works -- for k = 2:5 disp(k); end --- the same in different formulation --- v = 2:5; for k = v disp(k); end --- general form of the 'for' command v = [1 3 5 -2]; for k = v disp(k); end ### Example 2 ----- displaying the first five multiples of 4 ----- for k=1:5 m = k*4; disp(m); end --- the same with neat output ----- for k=1:5 m = k*4; fprintf('%d*4 = %d \n',k,m); end --- or even more neat ----- for k=1:5 m = k*4; fprintf('%d*4 = %2d \n',k,m); end --- solution without cycle ------- v = 1:5; v = 4*v; disp(v); ### Example 1 - continuing ----- computing the length of a broken line ----- n = input('give the number of the points: '); A = input('give the first point (for instance [ 1 5 7]): '); s = 0; % the length of the line for k=2:n B = input('give the next point: '); % input of the next point d = sqrt(sum((A-B).^2)); % length between the last two points s = s + d; % add it to the previous length A = B; % move the last point to the place of the previous point end disp('the length is'); disp(s); ### Example 3 ----- changing commentary during the cycle---- script reads 7 numbers and displays their running sum disp('give a sequence of 7 numbers'); s = 0; % the running sum for k = 1:7 fprintf('give the %d. number, please: ', k); a = input(''); s = s + a; fprintf('\ncurrent sum is %f \n\n', s); end ============================================= =========== the 'while' loop ================ ============================================= -------- how the 'while' command works -- n = 1; % used in the condition while n == 1 n = input('if you want to continue, enter 1, otherwise enter 2: '); end --- computing the number of loops n = 1; % used in the condition k = 0; % the number of loops while n == 1 k = k + 1; fprintf('this is %d. loop \n', k); n = input('if you want to continue, enter 1, otherwise enter 2: '); end ### Example 4 --- the running sum of a sequence terminated by zero --- disp('give a sequence of numbers, the stopping criterion is zero'); s = 0; % the running sum a = input('give the first number: '); while a ~= 0 s = s + a; fprintf('\ncurrent sum is %f \n\n', s); a = input('give the next number: '); end ### Example 5 --- the running sum of a sequence terminated by zero or sum >= 10 --- disp('give a sequence of numbers, the stopping criterion is zero'); a = input('give the first number: '); s = a; % the running sum while ((a ~= 0) && (s<10)) a = input('give the next number: '); s = s + a; fprintf('\ncurrent sum is %f \n\n', s); end ================================== =========== switch ============= ================================== ### Example 1 ---- write a given number verbally --- n = input('give a number from 4 to 7: '); switch n case 4 disp('four') case {5,6} disp('five or six') case 7 disp('seven') otherwise disp('an unexpected number') end --- comment ---- the same task can be accomplished using the 'if' command (however it is less neat): if n==4 disp('four') elseif (n==5)|(n==6) disp('five or six') elseif n==7 disp('seven') else disp('an unexpected number') end ---- switch can be used for logical variables, too: ### Example 2 ---- assign a grade to a test, given the number of points: the first case satisfying the condition is executed pts = input('give the number of points: '); switch true case pts>=86 display('grade A'); case pts>=77 display('grade B'); case pts>=68 display('grade C'); case pts>=59 display('grade D'); case pts>=50 display('grade E'); otherwise display ('grade F'); end; ================================================