% Illustrates use of the break statement to exit a loop immediately.

% Loop indefinitely ...
while 1
   % Generate a random integer from 1 to 10 and display it ...
   r = randi(10)

   % If the number is 5, exit the loop ...
   if r == 5
      fprintf('Breaking out of the while loop\n');
      break
   end
end

% Loop a maximum of 10000 times
for i = 1 : 10000
   % Generate a random integer from 1 to 10 and display it ...
   r = randi(10)

   % If the number is 3, exit the loop ...
   if r == 3
      fprintf('Breaking out of the for loop\n');
      break
   end
end
