% This mfile shows an example of the break statement clc clear all % For integers k=-10,-9,....,9,10, the function k^2-50 will take positive as % well as negative values. For example, for k=-9, k^2-50=31; for k=1, % k^2-50=-49; for k=8, k^2-50=14. % The loop below will calculate values of k^2-50 for all values of k. for k=-10:1:10 val=k^2-50; end % Let's suppose you are asked to calculate value of k^2-50 for % k=-10,-9,...,9,10 only until k^2-50 becomes negative. disp ('Using for statement') for k=-10:1:10 if (k^2-50<0) break; end val=k^2-50; fprintf('\n k=%g val=%g',k,val) end disp(' ') disp(' ') disp('Using while statement') % Equivalent in while k=-10; while (k<=10) & (k^2-50>0) val=k^2-50; fprintf('\n k=%g val=%g',k,val) k=k+1; end