Re: Use of nested loops.

From: Peter Pichler (pichlo6_at_pobox.sk)
Date: 11/03/03


Date: Mon, 3 Nov 2003 22:15:38 -0000


"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 :-)

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.



Relevant Pages

  • 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: 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: Why is C# 450% slower than C++ on nested loops ??
    ... Benchmarks should be verifiable .. ... A nested loop written the way it is in the benchmark is measuring nothing but a compiler's ability to optimize nested loops that do more or less nothing. ... The C++ compiler manages to pre-cache the additions in the outer loops, which the C# compiler doesn't. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Why is C# 450% slower than C++ on nested loops ??
    ... Benchmarks should be verifiable .. ... A nested loop written the way it is in the benchmark is measuring nothing but a compiler's ability to optimize nested loops that do more or less nothing. ... The C++ compiler manages to pre-cache the additions in the outer loops, which the C# compiler doesn't. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: What does "go to expert" mean?
    ... On 28/12/07 19:55, R H Draney wrote: ... Meaning: He's the best one to consult if you have a computer ... reduce it to two nested loops. ... The inner loop, which contained most of the code, ...
    (alt.usage.english)