Matlab-While loops -
given array have print numbers in array must positive , divisible 2, if condition not hold print "didn't find" once.
i have run code "didnt find" message entire length of array. how code says once?
here's code:
numbers = [9 3 7 3]; = 1; while <= length(numbers) if numbers(i)>0 && mod(numbers(i),2) == 0 disp(numbers(i)) else disp('didint find') = + 1; end end
numbers = [9 3 7 3]; found = false; %this boolean flag used see if have found number fitting rules. if find no number, found still false @ end of loop = 1:length(numbers) %a loop far more suited problem original while if numbers(i)>0 && mod(numbers(i),2) == 0 disp(numbers(i)) found = true; end end if ~found disp('didn''t find'); end
but in matlab no loop, in fact preferable not use loop. try following code:
ind = numbers > 0 & mod(numbers,2) == 0; if ~any(ind) disp('didn''t find'); else disp(numbers(ind)'); end
Comments
Post a Comment