0% found this document useful (0 votes)
9 views16 pages

Mireault ImplementingNestedFORLoopsV2

Uploaded by

Safwa Alia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views16 pages

Mireault ImplementingNestedFORLoopsV2

Uploaded by

Safwa Alia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/311255731

Implementing Nested FOR Loops as Spreadsheet Formulas

Conference Paper · July 2016


DOI: 10.1007/978-3-319-50230-4_31

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.

The user has requested enhancement of the downloaded file.


Implementing Nested FOR Loops as Spreadsheet
Formulas

Paul Mireault

SSMI International
Montréal, Canada
[email protected]

Abstract. A FOR loop is a computing structure that allows a set of calculations


to be made repeatedly for each iteration of the loop where the number of iterations
is known in advance. A nested loop happens when a loop is inside another loop.
In a spreadsheet program like Microsoft Excel, one can program loops in VBA,
its programming language. Spreadsheet developers who do not know how to pro-
gram in VBA usually implement the equivalent of loops with static values (e.g.
region codes and product types are typed as constants) or with formulas (e.g. the
region code is the previous region code + 1). In this paper, we present similarities
and differences between programming loops and spreadsheet formulas loops. We
also present a set of formulas that implement nested loops for 1, 2 or 3 nested
levels, along with a generalization for deeper nesting levels. We also provide
model management formulas to help the spreadsheet developer ensure that his
spreadsheet model covers all the iterations.

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

Spreadsheet developers sometimes find themselves in a situation where they need


to build a repetitive structure. For example, they may need to prepare a report or a model
with multiple dimensions like years, products and regions, as illustrated in Figure 1.
Figure 1 Example of a multi-dimensional structure

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.

3 Loops in computer programming

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

4.1 Simple loop

We will use the SSMI (Structured Spreadsheet Modelling and Implementation)


methodology described in [9] to illustrate the variables and the formulas that we will
implement in the spreadsheet.
In a simple loop, the value of the loop counter is incremented in each column. The
simple loop is illustrated in the Formula Diagram of Figure 2 and the Formula List of
Table 1.

Figure 2 Formula Diagram of the simple loop

Table 1 Formula List of the simple loop

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.

4.2 1-level nested loop

In a 1-level nested loop, we want to produce a result similar to Figure 4.

Figure 4 Illustration of a 1-level nested loop

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

Table 2 Formula List 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)

4.3 2-level nested loop

In a 2-level nested loop we want the loop indices to follow the pattern shown in
Figure 7.

Figure 7 Illustration of a 2-level nested loop

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

Table 3 Formula List 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

4.4 3-level nested loop

In a 3-level nested loop, we want the loop indices to follow the pattern shown in
Figure 10.

Figure 10 Illustration of a 3-level nested loop

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 4 Formula List of the 3-level nested loop

Variable Value / 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
Indicator J,1,0)
Increment J IF(Reset K Indicator = 1 AND Previous J <> Final
Indicator J, 1, 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(Reset L Indicator = 1 AND Previous K = Final
Indicator K,1,0)
Increment K IF(Reset L Indicator = 1 AND Previous K <> Final
Indicator K, 1, 0)
K IF(Reset K Indicator = 1,1,IF(Increment K
Indicator = 1, Previous K + 1, Previous K)),
Initial value = Final K
Previous L L(n-1)
Reset L IF(Previous L = Final L,1,0)
Indicator
L IF(Reset L Indicator = 1,1,Previous L + 1),
Initial value = Final L
We see, in the Formula Diagram and in the Formula List, that the middle loops have
the same set of 4 variables and similar formulas. Thus, this provides a generalization to
adding nested loops.
Table 5 summarizes the actions that can be performed on the different loop indices
and what their initial value must be.

Table 5 Actions that can be performed on a loop index depending on the loop's position

Reset Incremented Unchanged Initialization


Outer loop - X X 0
Middle loop X X X Final
Inner loop X X - Final

5 Model management formulas

Unlike programming, where the total number of iterations is determined by each


loop index’s final value and can change from one program execution to another, the
total number of iterations in a spreadsheet is determined by the number of columns in
which the loop formulas have been copied. If the spreadsheet developer copied the for-
mulas in 40 columns, that is good for loops with final values {2, 4, 5} and {5, 8} but
not for {3, 2, 7} and {9, 5}, which require 42 and 45 columns respectively.
While it would be possible to program a VBA (Visual Basic for Applications) mod-
ule to automatically adjust the number of columns according to the set of final values,
this is beyond the scope of this paper. Many spreadsheet developers have never taken
any programming course and would be incapable of programming such a VBA module.
The workaround we propose is to use model management formulas. We define a
model management formula as a formula whose purpose is to inform the spreadsheet
developer or user about errors or inconsistencies in the spreadsheet itself.
A simple model management formula will compare the expected number of columns
with the actual number of columns in the different spreadsheets that use the loop. The
expected number of columns is the product of the final values of the loops’ indices plus
the number of initialization columns. (That number is 1 most of the time, but a devel-
oper may need to adjust it to his needs.) The number of actual columns is the maximum
value of the loop iteration counter. The difference between those two numbers should
be zero, and it can inform the developer as to the number of columns to add or to remove
from the spreadsheets that use the loop. Figure 12 shows a warning indicating that the
loop formulas need to be copied in 31 more columns in two spreadsheets.

Figure 12 Model management formulas

6 Application to different contexts

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.

Figure 14 The Product Loop parameters


Figure 15 The Region loop parameters

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.

Figure 16 Creating the actual loops

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.

View publication stats

You might also like