MATLAB: Can any one tell how this break function works in this loop?

digital image processingdigital signal processingimage processingsignal processing

can any one tell me how the break functions if there are two if conditions in a for loop …
if true
% code
end
locs_Qwave=[ 100 150 90 1175 1 50]';
locs_Rwave=[116 170]';
q=0
k=1
for j=k:size(locs_Rwave)
for i=1:numel(locs_Qwave)
if (i== numel(locs_Qwave))
q=[q locs_Qwave(i)];
break;
end
if( locs_Qwave(i)>locs_Rwave(j))
q=[q locs_Qwave(i-1)];
break;
end
end
end

Best Answer

The second if statement is hit at j = 1, i = 2 so locs_Qwave(1) gets appended to q and it breaks out of the loop before the first if statement kicks in at all. Then we get j = 2 and now the second if statement kicks in at i = 4, adding locs_Qwave(3) to q. Again the first condition plays no part because it is not matched before the second condition matches and breaks out of the loop.
The first of condition appears to only be checking what would be the end of the for loop anyway so it will never activate before the final iteration. All it would do on its own is add a value to q after the for loop has ended which you could do without even having a for loop.
So with the other if statement in there too the first clause will only activate if the second clause is never matched.