%-------------------------------------------------------------------------------
% lecture8a
% continued from 7c
%-------------------------------------------------------------------------------
  clear
% more on
  more off
  clc
  echo on
% echo off
%-------------------------------------------------------------------------------
% Loops
% + repetition statements
% + Now show way of doing manual multiple values
% + used to repeat operations
% + major reason for computers in the first place!
%-------------------------------------------------------------------------------
% WHILE loop
% From $help while$
%
% WHILE  Repeat statements an indefinite number of times.
%    The general form of a WHILE statement is:
%  
%       WHILE expression
%          statements
%       END
%  
%   The statements are executed while the real part of the expression
%   has all non-zero elements. The expression is usually the result of
%   expr rop expr where rop is ==, <, >, <=, >=, or ~=.
% 
%   The BREAK statement can be used to terminate the loop prematurely.
%-------------------------------------------------------------------------------
pause
clc
% example) count numbers from 1 to n

  x = 0; % x "so far"
  stop = input('enter stopping value n: ');
   
  % note:
  % variables that generalize a program $stop$ are called
  % NAMED CONSTANTS -- use them in as many palces as you can

  % output column of values from 1 to $stop$

  while x < stop 
    x = x + 1 % increment x by 1
    pause
  end

  x % what's x when loop ends?

  % notes:
  % -----
  % boundary conditions -- I have loop possibly stopping for a count of
  %    zero. That's a kind of boundary condition, or limiting value
  %    Actually, the result isn't too pretty if you enter 0 for $stop$.
  %    MATLAB returns the empty matrix $[]$
  % 
  % vectorizing -- overall my style is OK here, but there's some efficiency
  %    problems. You don't really need a loop to gather a count of values!
  %    Vectorizing code means finding a way for MATLAB's array to
  %    supercede a loop. Using built-in arrays generally works faster than
  %    processing a loop. Not all languages have this feature, though.

%-------------------------------------------------------------------------------
% Patterns to loops
% + (repetition) repeating the same procedure over and over
% + (conditional update) finding or sorting or searching for a value
% + (accumulation) summing values
%
% see lectures 8b,8c,8d
%-------------------------------------------------------------------------------
% FOR
% From MATLAB $help for$:
%
% FOR    Repeat statements a specific number of times.
%    The general form of a FOR statement is:
%  
%       FOR variable = expr, statement, ..., statement END
%
% Use $for$ when the loop is determined -> you know when it will end
% Sometimes you might wish to use $break$ to get out of loop
% + in those cases, really should use $while$
% + $while$ is "most pure" loop structure
% + $while$ tends to avoid the "evil" $break$

% ex) an entire $for$ statement
for ii=1:3
  ii
end

% Nested for-loop
% + put loop inside another loop
% + the inner loops finish and return control back to outer loops

clear tmp
for ii=1:3
  str = ''
  for jj=1:4
      str = strcat(str,num2str(ii),num2str(jj),'_')
  end
  tmp(ii,:) = str % store results in array of strings
end
tmp
