Re: Use of nested loops.
From: Neptune (neptune_at_no_spam_here)
Date: 11/04/03
- Next message: Tim Woodall: "Re: "Mastering C Pointers"...."
- Previous message: Keith Thompson: "Re: "Mastering C Pointers"...."
- In reply to: Peter Pichler: "Re: Use of nested loops."
- Next in thread: Irrwahn Grausewitz: "Re: Use of nested loops."
- Reply: Irrwahn Grausewitz: "Re: Use of nested loops."
- Reply: Keith Thompson: "Re: Use of nested loops."
- Reply: Glen Herrmannsfeldt: "Re: Use of nested loops."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Tim Woodall: "Re: "Mastering C Pointers"...."
- Previous message: Keith Thompson: "Re: "Mastering C Pointers"...."
- In reply to: Peter Pichler: "Re: Use of nested loops."
- Next in thread: Irrwahn Grausewitz: "Re: Use of nested loops."
- Reply: Irrwahn Grausewitz: "Re: Use of nested loops."
- Reply: Keith Thompson: "Re: Use of nested loops."
- Reply: Glen Herrmannsfeldt: "Re: Use of nested loops."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|