MATLAB: Is this program free from bugs? The program is to find the position of the element greater than 100.

bugsfor loop

%% Example
for ii= 1:length(readings)
if readings(ii)>100
fprintf('first reading above 100 is at position %d\n', ii);
return;
end
end
fprintf('no values greater than 100 upto position %d\n',ii);

Best Answer

  • The program will not work properly if readings is two or more dimensional.
    The program will fail when it encounters the return statement: return is only permitted in functions.
    The program will fail if readings is something that is not comparable to a number, such as if it is a cell array or transfer function.