N
Neptune
Peter said:Neptune, in many cases you use multiple nested loops without even knowing
it.
Let's look at Keith's a modified example. Open a text file and read and
each line. You need a loop in which to read a line, test you have
successfully
read it and then print it. Inside that loop, you would probably use fgets
for
reading and puts or printf for printing. Each of these functions contains at
least one loop, possibly more than one. So, you may get nested loops simply
by
calling a function in a loop. Isn't programming fun![]()
It is fun - that's the attraction (once I get my head around some basic
constructs that is
touches on something that I will be coming to in due course, but was
already curious about: readin in text files, so this will pre-'arm' me
so to speak.
To answer your original question, nested loops are used whenever an
algorithm
asks for it. Processing more than one-dimensional entities is the most
common
example. My example is one of them: the line number could be considered the
vertical and the position of a character in a line the horizontal dimension.
It appears then that (theoretically speaking anyway), there are no
limits to the number of loops one can nest? I cannot think of any
example to illustrate this, but was wondering if one can nest one or two
or five loops, is there a maximum to which one can go before it
discombobulates entirely. I can imagine that there would be logistical
nightmares (indentation running off of the page or line wrapping
endlessly, trying to keep the iterations straight in one's head, even
finding a value to engage in that kind of exercise, etc), so the query
really is theoretical.
Anyway, thanks for your thoughts.
--
"Today a young man on acid realised that all matter was really energy
condensed to a slow vibration, that we are all one consciousness
experiencing itself subjectively, there's no such thing as death,
life is only a dream, and we're the imaginations of ourselves.
Here's Tom with the weather ..." - Bill Hicks.