Terminating a Loop Prematurely: Break and Continue

As you may recall, a while loop will evaluate all its statements without checking the condition. Similarly a for loop will run through all of its iterations. The break keyword tells MATLAB® to exit the loop immediately. It will only terminate one loop (in the case of nested loop, the innermost one it is in) and will normally be protected by an if statement (otherwise the loop is silly). Here is an example that computes the "trajectory" of 6 but stops if it finds a 17 in it:

s=6;             % initialize s to 6 while s~=1       % as long as s is not equal to 1 stay in the loop      if s==17    % if s equals 17           sprintf('Found 17 in the loop!!')           break;      end      if mod(s,2) % the actual "brains" of the iteration           s=s/2;      else           s=3*s+1;   
     end end

The keyword continue is similar but different. It avoids the rest of the statements of the inner most loop, but continues in the loop (does not stop like break).

Here's example code for a while loop that uses both break and continue to find the first 100 primes (not very efficiently, but it's only an example):

n=1; m=0;  while 1      % this means that unless we use "break", the loop will continue "forever"      n=n+1;  % increase n      flag=0; % reset flag      for i=2:ceil(sqrt(n)) % no need to check numbers greater than the square-root of n           if mod(n,i)==0   % means that i divides n exactly                flag = 1    % to know that we found a divisor                break;      % no need to remain in the for loop           end      end      if flag           continue % to avoid the next line. It could have also been done                    % differently with an "if" statement, but this can be more elegant      end      sprintf('%d is prime!\n',n) % this is quite an interesting command...                                  % take some time to learn about it      m=m+1;      % increment primes count      if m>=100   % if we have enough           break; % stop looking for primes      end end 

Homework 6. The keywords break and continue are not "needed'' per se, but they can make the code more elegant and readable. Rewrite the above code for the first 100 primes without using neither continue nor break.

Homework 7. for loops and while loops are not inherently different:

  • The "input" of a for loop is a variable and a vector of values. Recreate the functionality of a for loop using a while loop.
  • The "input" of a while loop is the condition statement. Recreate the functionality of a while loop using a for loop. (Hint: when using the notation for i=1:n MATLAB does not actually create the vector 1:n. Internally it simply iterates over the values in that vector by incrementing i until it reaches n. This means that if you write for i=1:281474976710655 you'll get a loop that, on its own, will "never" terminate. Explanation: 281474976710655 is the largest integer that MATLAB can represent internally. It is such a large number that even if every pass through the loop only takes 1 millisecond getting through the loop will take about 10000 years.)