Re: Use of nested loops.

From: Neptune (neptune_at_no_spam_here)
Date: 11/04/03


Date: Tue, 04 Nov 2003 22:40:26 +0000

Peter Pichler wrote:
> "Keith Thompson" <kst@cts.com> wrote in message
> news:lzism1qhh4.fsf@cts.com...
>
>>BTW, another example of a nested loop might be reading lines from a
>>file, and processing each character in each line. You might use a
>>triple-nested loop to open each of the files named on the command
>>line, reading each line from each file, and processing each character
>>on each line.
>
>
> 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
> print
> 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 :) ). I like your example Peter - it makes sense and
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.


Relevant Pages

  • Re: Problem with a script
    ... a loop there becomes impractical. ... You still have them as uniquely named array indexes... ... writing the code twice will only ... reading your entire code and parsing it in their head, ...
    (comp.lang.php)
  • Re: ReadLine weirdness!
    ... I changed my code to use Read and loop through the line ... reading one character at a time instead of the whole line with Readline and ... > instinct is to suspect your drive or file system. ... >>support this character yet I can see the line as it should be if I open it ...
    (microsoft.public.access.modulesdaovba)
  • Re: Problem with a script
    ... Okay, so variables have unique labels, that doesn't mean they still couldn't be handled in a loop. ... You still have them as uniquely named array indexes... ... I believe that for the new guy this code would be readable, and identifying problems should really not be any more difficult with this, plus I think that it actually might save some time to write the actual code from the beginnig, even though it's not at it's final stage, instead of first writing everything spread out, and then rewriting the same code again cleaned. ... If you expect a person to spend an hour reading your entire code and parsing it in their head, you wont get any help and have to solve the problem by yourself. ...
    (comp.lang.php)
  • Re: StreamReader cutting off first character
    ... stream, and when there are no more characters in the stream it returns -1. ... If you would have a blank file, the loop would not execute, because the ... when it doesn't stop reading the file when it reaches the last ... >> think that sr.Read is getting the first character in each line, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: advice and/or help on glReadPixels
    ... I could not see any "BRGA" in the documentation. ... Is it possible that some cards are not ... >>> a) Try reading the entire RGB instead of just the red. ... a quick integer loop with a mask & shift is ...
    (comp.graphics.api.opengl)