Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Loop Programming Patterns

Loop Programming Patterns

Ratings: (0)|Views: 11|Likes:
Published by Secret Squirrel

More info:

Published by: Secret Squirrel on Jun 12, 2013
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





4/27/01Page 1 of 7
Loop Programming Patterns
by John R. Glover 
A software pattern
is a reusable solution to a recurring problem that arises within acertain context and system of forces. Patterns can be described at three levels:
architectural patterns
design patterns
, and
 programming patterns
Programming patterns
coding patterns
) are low-level patterns specific to a programming language. Experienced programmers automatically call upon these patternswhen they see that one is appropriate to the task.A pattern has (at least) a
, a
to be solved, a standard
, and
 that illustrate it. (For these low-level patterns, we will omit the context and forces.)Let's look at a few. Since these are all loop programming patterns, for simplicity theywill be expressed in terms of problems involving values in an array.
Name: Loop Condition
Problem: A test must be applied to each value in a vector.Solution: Use an
statement to apply the test inside a
% Display all negative values in the vectorfor n = 1:length(x)
if x(n) < 0
Name: Counter
Problem: It is necessary to count occurrences of some condition in the loop.Solution: Maintain a separate counter variable.Example:
% Count the negative values
counter = 0;
%initialize counterfor n = 1:length(x)if x(n) < 0
count = count + 1;
%increment counterendend
4/27/01Page 2 of 7
Name: Compute from Index
Problem: A value must change with each pass through the loop, but that value is not theloop variable.Solution: Compute the value using the loop variable.Example:
% Compute values of sin(t)for n = 1:101
t = (n-1)*2*pi/100;
%compute from loop variabley(n) = sin(t);end
Name: Separate Index
Problem: The loop variable is not suitable for an array index, but one is needed.Solution: Maintain a separate array index.Example:
% Compute values of sin(t)
n = 1;
%initialize separate indexfor t = 0:2*pi/100:2*piy(n) = sin(t);
n = n + 1;
%increment indexend
Name: Second Index
(similar to Separate Index)
Problem: It is necessary to index through two arrays at a different rate, so the loopvariable can be used for only one.Solution: Maintain a second array index incrementing at the different rate.Example:
% Copy just the negative values into a new vector
index2 = 1
; %initialize second indexfor n = 1:length(x)if x(n) < 0y(
) = x(n);
index2 = index2 + 1
; %incr second indexendend
4/27/01Page 3 of 7
Name: Flag Variable
Problem: It is necessary to mark the occurrence of an event in the loop so that you knowit has occurred, either on the next pass through the loop or when you exit the loop.Solution: Maintain a Boolean flag variable. The flag's state indicates whether the eventhas occurredExample (On exit from loop, the flag indicates whether the event occurred in the loop.):
% Look for a specified value in the vectortarget = input('Enter value to be found:');
found = 0;
%not found yetfor n = 1:length(x)if x(n)==target
found = 1
; %foundendend
if found 
…do something…
Example (On each pass through the loop, the flag indicates whether the event occurred ona previous pass through the loop, perhaps putting the code in a different "mode."):
% How many times does a positive follow a negative% in the vector x?count = 0;
negfound = 0; % flag=1 if negative found last pass
 for n = 1:length(x)% First, was a negative found on previous pass?
if negfound 
if x(n)>0 %...yes, so is this now a positive?count = count + 1;endend% In any case, set flag for next pass
negfound = x(n)<0
; % true only if this value negativeenddisp(count)

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->