Mireault ImplementingNestedFORLoopsV2
Mireault ImplementingNestedFORLoopsV2
net/publication/311255731
CITATIONS READS
3 200
1 author:
Paul Mireault
SSMI International
17 PUBLICATIONS 198 CITATIONS
SEE PROFILE
All content following this page was uploaded by Paul Mireault on 04 May 2023.
Paul Mireault
SSMI International
Montréal, Canada
[email protected]
1 Introduction
Various research has reported important spreadsheet error rates. [1] surveyed studies
showing a percentage of spreadsheet with errors as high as 86%. Spreadsheet errors
have led to not only monetary losses [2] but have also caused career failures [3] and
reputation losses [4].
Research has been done to help spreadsheet developers build complex spreadsheets
by using Computer Science and Software Engineering concepts. For example, [5] stud-
ied the use of user-defined functions as a way to reduce formula complexity, [6] exam-
ines cell labels typed by the spreadsheet developer to infer a structure and provide type
checking in formulas, and [7] proposes a model-driven approach. Finally, [8] developed
a methodology based on the conceptual model of Information Systems.
2 Structure in spreadsheets
We have seen spreadsheet developers build such structures by typing values and
repeatedly copying them to achieve the desired result. Some other developers type val-
ues and use reference formulas to produce the same result. While the latter approach
offers some flexibility by allowing the user to easily change a dimension’s values, both
approaches don’t allow the user to easily add or remove values in the dimensions.
In a computer program, the same structure is normally implemented with nested
loops. Any modification requires only that the user add or remove values and re-execute
the program.
The objective of this paper is to devise a set of formulas to implement nested loops
in a spreadsheet using nothing else but a plain version of Excel (i.e. not using external
spreadsheet generator or add-ins.) We will start with a simple loop and proceed to a 1-
level, 2-level and 3-level nested loop, explaining the how to implement each level’s
specific characteristics. Finally, we will illustrate how to adapt the general loop imple-
mentation to situations more suitable to general spreadsheet uses.
Loops are a programming construct that cause the execution a series of instructions
to be repeated a number of times. During the loop’s execution, variables are assigned
values and these values are overwritten in each execution.
Implementing a loop in a spreadsheet consists of setting up the iterations in columns
or in rows. Since a spreadsheet is a static object, columns cannot be added program-
matically during its utilization: the spreadsheet developer must create the appropriate
number of columns.
A FOR loop is used when the number of iterations is known before its execution. It
uses a loop counter which is initialized at the beginning of the loop. The loop instruc-
tions are then executed and the loop counter is then incremented by a specified value.
If the loop counter does not exceed the maximum specified value, the loop is repeated.
In its simplest case, the loop counter is initialized with the value 1 and it is incremented
by 1 at the end of each iteration.
C Java BASIC
for ( i = 1; i <= n; i++) for ( i = 1; i <= n; i = i+1) FOR I = 1 TO N
{set of instructions} {set of instructions} set of instructions
NEXT I
A nested loop is a loop that occurs within another loop, as illustrated in the following
code snippet:
BASIC
FOR I = 1 TO N
FOR J = 1 TO M
set of instructions
NEXT J
NEXT I
The total number of iterations, i.e. the number of times the set of instructions will be
executed, is N*M. As we nest more loops, the number of iterations can become quite
large quickly. In a computer program this only affects the time it takes to execute the
program. But in a spreadsheet, this has the effect of adding columns (or rows) to the
spreadsheet.
4 Loops in a spreadsheet
Variable Formula
Previous I I(n-1)
I Previous I + 1, Initial value = 0
In the SSMI methodology, all formulas should refer to cells in the same column.
When we need to use a value in the previous column, we create a variable whose sole
purpose is to reference that value. This way, all references to other columns are well
indicated. In this case, we create a variable, Previous I = I(n-1), to record the
value variable I had in the previous column and use it to calculate the value of the loop
index, I = Previous I + 1. Since we refer to one column on the left, we need to
provide an initialization column to provide the initial values for the variables that are
the object of a (n-1) reference. Figure 3 shows the SSMI implementation of the simple
loop: column A contains the variable names, column B is the initialization column and
columns C to G represent the loop iteration. Also, a variable is named in the row or
column where it is defined, and a visual cue (bold italic) is used to show exactly where
an Excel name has been created, such as Previous I and I in rows 6 and 9. 9
Figure 3 Implementation of the simple loop, normal view (left) and formula view (right)
Since we want the loop counter to start at 1, we explicitly initialize it to 0 in cell B9.
In this example, the inner loop index J has 2 values and the outer loop index I has 5
values. We can observe that whenever J reaches its final value it resets to 1, and when
it resets, the value of I gets incremented, otherwise it remains unchanged.
To model that behavior, we introduce an indicator variable, Reset J Indicator,
which takes the value 1 when the counter J needs to be reset and the value 0 otherwise.
The condition for a reset is that the previous value of J has reached its final value. This
is illustrated in the Formula Diagram of Figure 5 and the Formula List of Table 2.
Figure 5 Formula Diagram of the 1-level nested loop
Variable Formula
Previous I I(n-1)
I IF(Reset J Indicator = 1,Previous I + 1,
Previous I), Initial value = 0
Previous J J(n-1)
Reset J IF(Previous J = Final J,1,0)
Indicator
J IF(Reset J Indicator = 1,1,Previous J + 1),
Initial value = Final J
Since the loop counter I is initialized at 0 we need an initial condition to have the
Reset J Indicator set to 1 immediately in the first iteration. To do so, we initialize
J with its final value. Figure 6 shows the SSMI implementation of the 1-level nested
loop.
Figure 6 Implementation of the 1-level nested loop, normal view (left) and formula view (right)
In a 2-level nested loop we want the loop indices to follow the pattern shown in
Figure 7.
In this example, the inner loop index K has 4 values, the middle loop index J has 2
values and the outer loop index I has 5 values. We can observe that the behavior of the
inner loop index hasn’t changed: whenever it reaches its final value it resets to 1. The
same can be said about the outer loop index: when the next loop index J resets, the
value of I gets incremented, otherwise it remains unchanged. But now, the middle loop
index J has 3 possibilities at each iteration: it remains unchanged, it is incremented or
it is reset. We need two indicator variables to indicate when it should be incremented
and when it should be reset. The condition for a reset is when K has been reset and
Previous J has reached its final value. The condition for an increment is when K has
been reset and Previous J has not reached its final value. This is illustrated in the
Formula Diagram of Figure 8 and the Formula List of Table 3.
Figure 8 Formula Diagram of the 2-level nested loop
Variable Formula
Previous I I(n-1)
I IF(Reset J Indicator = 1,Previous I + 1, Previous I),
Initial value = 0
Previous J J(n-1)
Reset J IF(Reset K Indicator = 1 AND Previous J = Final J,1,0)
Indicator
Increment J IF(Reset K Indicator = 1 AND Previous J <> Final J, 1,
Indicator 0)
J IF(Reset J Indicator = 1,1,IF(Increment J Indicator = 1,
Previous J + 1, Previous J)), Initial value = Final J
Previous K K(n-1)
Reset K IF(Previous K = Final K,1,0)
Indicator
K IF(Reset K Indicator = 1,1,Previous K + 1), Initial
value = Final K
Figure 9 shows the SSMI implementation of the 2-level nested loop.
Figure 9 Implementation of the 2-level nested loop
In a 3-level nested loop, we want the loop indices to follow the pattern shown in
Figure 10.
In this example, the inner loop index L has 3 values and the other loops are as before.
We can observe that the behavior of the middle loops J and K are the same: whenever
their previous loop index reaches its final value they either reset to 1 or increment,
otherwise they remain unchanged. This is illustrated in the Formula Diagram of Figure
11 and the Formula List of Table 4.
Figure 11 Formula Diagram of the 3-level nested loop
Table 5 Actions that can be performed on a loop index depending on the loop's position
In practice, spreadsheet developers use loops to represent objects that don’t have the
standard characteristics of the loops we used so far. Their first value may not always be
a 1, and in some cases they may not even be quantitative. In this section we show two
common examples: in the first case the loop models a set of years, and in the second
case it models a set of qualitative variables.
Figure 13 shows the Year Loop parameters, where the user can enter the first and
last years that the spreadsheet should consider. Mode management formulas calculate
the Number of Years, which is used as Final I, and the Base Year, which will be
explained shortly.
Figure 13 The Year Loop parameters
Figure 14 shows the Product Loop parameters. In this case, the user can enter product
names in the List of Products and the Number of Products is calculated as the
number of elements in the list and is used as Final J. Figure 15 shows the Region
Loop parameters which behaves similarly and supplies the value of Final K.
After having prepared the different loop parameters, we can create the user’s loops,
as shown in Figure 16. We can use Excel’s INDEX function with the J and K loop indices
because their starting value is always 1. The Year is calculated as I + Base Year.
Once the nested loops are set, the developer can use them in other worksheets. If the
worksheet has the same column structure as the Y-P-R Loop worksheet, we can simply
use the loop index’s name as shown in Figure 17.
Figure 17 Using the loop indices in a worksheet with the same column structure
On the other hand, if the worksheet does not have the same column structure, the
developer can create a row referring to Y-P-R Iteration Counter and use it with
the INDEX function as shown in Figure 18.
Figure 18 Using the loop indices in a worksheet with a different column structure
7 Conclusion
The worksheets managing the loops are behind the scene activities. Once they have
been implemented and tested, the developer can concentrate on the actual worksheet
that needs to be built.
This paper shows that a well-known computer programming structure can be adapted
to be used in the spreadsheet environment. The adaptation has its limitations, the most
important one being that the spreadsheet’s structure has to conform to the desired loops.
This paper contains all the formulas spreadsheet developers need to create their own
worksheets to manage loop-like structures, whether they have programming training or
not. They can be saved and re-used or distributed as templates.
8 References
[1] R. R. Panko, "What We Know About Spreadsheet Errors," Journal of End User
Computing, vol. 10, pp. 15-21, 2008 (Revised).
[2] T. Burden. (2013). How A Rookie Excel Error Led JPMorgan To Misreport Its VaR
For Years. Available: http://www.zerohedge.com/news/2013-02-12/how-
rookie-excel-error-led-jpmorgan-misreport-its-var-years
[3] (2011). Mouchel profits blow. Available:
http://www.express.co.uk/finance/city/276053/Mouchel-profits-blow
[4] M. Conczal. (2013). Researchers Finally Replicated Reinhart-Rogoff, and There Are
Serious Problems. - Roosevelt Institute. Available:
http://rooseveltinstitute.org/researchers-finally-replicated-reinhart-rogoff-
and-there-are-serious-problems/
[5] S. P. Jones, A. Blackwell, and M. Burnett, "A user-centred approach to functions in
excel," ACM SIGPLAN NOTICES, vol. 38, pp. 165-176, 2003.
[6] M. Erwig, "Software Engineering for Spreadsheets," IEEE Software, vol. 26, pp. 25-
30, 2009.
[7] J. Cunha, J. P. Fernandes, J. Mendes, and J. Saraiva, "MDSheet: a framework for
model-driven spreadsheet engineering," presented at the Proceedings of the 34th
International Conference on Software Engineering, Zurich, Switzerland, 2012.
[8] P. Mireault, "Structured Spreadsheet Modeling and Implementation," in 2nd Workshop
on Software Engineering Methods in Spreadsheets, Firenze, IT, 2015.
[9] P. Mireault, Structured Spreadsheet Modelling and Implementation: A Methodology
for Creating Effective Spreadsheets. Montréal: SSMI International, 2016.