%%Break and Continue
%Disclaimer: In general, neither of these techniques are recommended as
%they can difficulty with debugging. In addition, you can usually rewrite
%your code to avoid using either of these statements. (I am guilty of using
%these statements...)


%Can be used in both for and while loops.

%break - stops current iteration of the loop, skips the rest of the
%statements, and ends the loop.
% Only effects the loop where it occurs (nearest enclosing loop)

%continue - stops current iteration of the loop, skips the rest of the
%statements, and starts the next iteration of the loop.
% Only effects the loop where it occurs (nearest enclosing loop)


%Example 1: break when a number is not a perfect square.
%mod(a,m) returns remainder after division of a by m
%ex: mod(8,3) returns 2
for i = [4 9 16 20 24 36 49]
  if mod(sqrt(i), 1) ~= 0
    break
  end
  disp(sqrt(i))
end


%Example 2: continue when a number is not a perfect square.
for i = [4 9 16 20 24 36 49]
  if mod(sqrt(i), 1) ~= 0
    continue
  end
  disp(sqrt(i))
end

%Example 3: multiplication table with 0's on the diagonal
tablesize = 10;
multtable = zeros(tablesize);
for r=1:tablesize
  for c=1:tablesize
    if(r == c)
      continue
    end
    multtable(r,c) = r*c;
  end
end
disp(multtable)

%Example 4: Only bottom left part of multiplication table
tablesize = 10;
multtable = zeros(tablesize);
for r=1:tablesize
  for c=1:tablesize
    if(r == c-1)
      break
    end
    multtable(r,c) = r*c;
  end
end
disp(multtable)



