7/28/2019 Loop Programming Patterns
1/7
4/27/01
Page 1 of 7
Loop Programming Patterns (preliminary)by Jo hn R. Glover
A software pattern is a reusable solution to a recurring problem that arises within a
certain context and system of forces. Patterns can be described at three levels:architectural patterns, design patterns, andprogramming patterns.
Programming patterns (idioms , coding patterns) are low-level patterns specific to aprogramming language. Experienced programmers automatically call upon these patterns
when they see that one is appropriate to the task.
A pattern has (at least) a name, aproblem to be solved, a standardsolution, and examplesthat 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 they
will be expressed in terms of problems involving values in an array.
Name: Loop ConditionProblem: A test must be applied to each value in a vector.
Solution: Use an if statement to apply the test inside a for loop.Example:
% Display all negative values in the vector
for n = 1:length(x)if x(n) < 0
disp(x(n))
end
end
Name: CounterProblem: It is necessary to count occurrences of some condition in the loop.
Solution: Maintain a separate counter variable.Example:
% Count the negative valuescounter = 0; %initialize counter
for n = 1:length(x)
if x(n) < 0count = count + 1; %increment counter
endend
7/28/2019 Loop Programming Patterns
2/7
4/27/01
Page 2 of 7
Name: Compute from IndexProblem: A value must change with each pass through the loop, but that value is not the
loop variable.Solution: Compute the value using the loop variable.
Example:
% Compute values of sin(t)for n = 1:101t = (n-1)*2*pi/100;%compute from loop variable
y(n) = sin(t);
end
Name: Separate IndexProblem: 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 index
for t = 0:2*pi/100:2*pi
y(n) = sin(t);n = n + 1; %increment index
end
Name: Second Index (similar to Separate Index)Problem: It is necessary to index through two arrays at a different rate, so the loop
variable 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(index2) = x(n);index2 = index2 + 1; %incr second index
end
end
7/28/2019 Loop Programming Patterns
3/7
4/27/01
Page 3 of 7
Name: Flag VariableProblem: It is necessary to mark the occurrence of an event in the loop so that you know
it 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 event
has occurred
Example (On exit from loop, the flag indicates whether the event occurred in the loop.):
% Look for a specified value in the vector
target = input('Enter value to be found:');found = 0; %not found yet
for n = 1:length(x)
if x(n)==targetfound = 1; %found
end
endif found
do something
end
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 negfoundif x(n)>0 %...yes, so is this now a positive?
count = count + 1;
end
end
% In any case, set flag for next passnegfound = x(n)
7/28/2019 Loop Programming Patterns
4/7
4/27/01
Page 4 of 7
Name: Early ExitProblem: Part way through the loop, there is no longer a need to continue it.
Solution: Exit early when the exit condition is detected (Flag Variable is probablyneeded so that you will know that the early exit occurred).
Example:
% Look for a specified value in the vectortarget = input('Enter value to be found:');
found = 0; %not found yet
for n = 1:length(x)
if x(n)==target
found = 1; %foundbreak;
end
end
if founddo something
end
Name: Early Return
Problem: Part way through execution of a function, the answer is computed and there isno need to continue the function.Solution: Return immediately without executing the remainder of the code.
Example:function result = sumsquares(x)
% SUMSQUARES Return avg of squares of x values unless
% one of them is negative, in which case return zero
total = 0;
for n = 1:length(x)if x(n)
7/28/2019 Loop Programming Patterns
5/7
4/27/01
Page 5 of 7
Name: Extreme ValueProblem: A value must be located that stands out from every other value encountered in
the loop.Solution: Maintain a separate extreme so farvariable, initialized to the first value.
Example:
% Find the largest value in the vectormaxval = x(1); %largest so far
for n = 2:length(x) %continue with 2nd
valueif x(n) > maxvalmaxval = x(n); %replace with new max value
end
end
Name: Extreme Initial Value
Problem: We want to know which value best meets a condition, but we cannot easily (ordo not wish to) initialize to a first candidate.Solution: Initialize the answer to an extreme or impossible value, knowing that it will be
updated on the first pass through the loop.Example:
% Find the largest absolute value in the vectorabsmax = -1; %will be changed immediately
for n = 1:length(x) %start with the first
if abs(x(n)) > absmax
absmax = abs(x(n)); %replace
end
end
Name: Save Index
Problem: The result desired is the index of the array value meeting the condition.Solution: Use a separate index variable to save the desired index.Example:
% Clear the last negative value in the vector to zerosaveIndex = 0; %no index yet
for n = 1:length(x)
if x(n) < 0saveIndex = n; %save new index
end
endif saveIndex > 0
x(saveIndex) = 0; %clear value at last indexend
Note: This is combined with a variation of the Flag Variable pattern, since the nonzerovalue of saveIndex indicates that a negative value was found.
7/28/2019 Loop Programming Patterns
6/7
4/27/01
Page 6 of 7
Name: Factor Out InvariantsProblem: In a loop it is necessary to repeatedly use the result of a function call or other
computation, although that result does not change.Solution: Improve efficiency by taking the function call or computation outside the loop.
Example, inefficient solution:% Find the value closest to target value: cos(pi/5)closest = x(1); %closest so far
for n = 2:length(x) %continue with 2nd
valueif abs(x(n)-cos(pi/5)) < abs(closest-cos(pi/5))
closest = x(n); %save new closest value
end
end
disp(['Closest is:' num2str(closest)])
Example, factoring out the invariant computations :
% Find the value closest to target value: cos(pi/5)
closest = x(1); %closest so fartarget = cos(pi/5);distance = abs(closest-target);
for n = 2:length(x) %continue with 2nd
valueif abs(x(n)-target) < distance
closest = x(n); %save new closest value
distance = abs(closest-target); % and new distance
end
end
disp(['Closest is:' num2str(closest)])
Note: The Extreme Value pattern is also used in this example.
7/28/2019 Loop Programming Patterns
7/7
4/27/01
Page 7 of 7
Name: Swap/ExchangeProblem: We need to exchange two values in a vector.
Solution: Perform the exchange through a separate swap variable.Example:
% Reverse the elements in a vector
for n = 1:fix(length(x)/2)m = length(x)-n+1; %index backwards from endswap = x(n);
x(n) = x(m);x(m) = swap;
end
Note: The Second Index pattern is also used in this example.Note: Efficiency of this example could be improved by using the Factor Out Invariantspattern:
% Reverse the elements in a vector
len1 = length(x)+1; %precompute invariant
for n = 1:fix(length(x)/2)
m = len1-n; %index backwards from endswap = x(n);
x(n) = x(m);x(m) = swap;
end
Name: Two QuestionsProblem: We want a complex result that is not easily answered by a single pattern.Solution: Recognize that two (or more) questions are really being asked, and devise the
appropriate combination of patterns as the solution.Example:
% Are there more than 25 values greater than 100?count = 0; %init counterfound = 0; %init flag
for n = 1:length(x)
if x(n) > 100count = count + 1;
if count > 25found = 1;
break;
endend
endif found
disp('Yes, more than 25 values were found.')
end
Note: Patterns used are Counter, Flag Variable, andEarly Exit.
Top Related