Use of nested loops.

B

Ben Pfaff

Morris Dovey said:
Hmmm. How about:

Larry meant what he said. Read C99 6.8.5:

5 An iteration statement is a block whose scope is a strict
^^^^^^^^^^
subset of the scope of its enclosing block. The loop body is
also a block whose scope is a strict subset of the scope of
the iteration statement.

The corresponding section in C90 doesn't include anything similar.
 
M

Morris Dovey

Alex said:
I count 6 block scopes. In C99 you could have declared i, j, and k
within the initialization statement of the for loop. IIRC, the body
of a loop opens a new block scope. This does not necessitate the
use of curly brackets.

I could have, but didn't. I can make a block (with curly braces)
anywhere I can code a statement. Are you telling me that there
/is/ a block everywhere there /can/ be a block, even though I
don't code the curly braces - or are you simply reminding me that
the potential for creating all these blocks is lurking there? Is
there block scope without a block?
 
B

Ben Pfaff

Morris Dovey said:
I could have, but didn't. I can make a block (with curly braces)
anywhere I can code a statement. Are you telling me that there /is/ a
block everywhere there /can/ be a block, even though I don't code the
curly braces - or are you simply reminding me that the potential for
creating all these blocks is lurking there? Is there block scope
without a block?

My guess is that C99 has every iteration statement open a block
because `for' statements in C99 can have their own local
variables (declared in the first clause).
 
A

Alex

Morris Dovey said:
Alex wrote:
I could have, but didn't. I can make a block (with curly braces)
anywhere I can code a statement. Are you telling me that there
/is/ a block everywhere there /can/ be a block, even though I
don't code the curly braces - or are you simply reminding me that
the potential for creating all these blocks is lurking there? Is
there block scope without a block?

I am saying that there is /block scope/ at the places which I pointed
out even though there aren't actual physical curly blocks there.

Ben Pfaff just posted a quote from the standard which seems to
support my assertion.

Alex
 
K

Keith Thompson

Morris Dovey said:
I could have, but didn't. I can make a block (with curly braces)
anywhere I can code a statement. Are you telling me that there /is/ a
block everywhere there /can/ be a block, even though I don't code the
curly braces - or are you simply reminding me that the potential for
creating all these blocks is lurking there? Is there block scope
without a block?

The definition of "block" changed from C90 to C99. In C90, a block is
an optional declaration list, followed by an optional statement list,
all surrounded by curly braces. In C99, not all blocks have curly
braces. So the following:

while (1) printf("I will not write infinite loops\n";

contains no blocks in C90, but two blocks (the loop and its body) in
C99.

I think the change was made because of the addition of declarations
in for loops:

for (int i = 1; i <= 10; i ++) ...

The loop was defined to be a block to provide a scope for the
declaration of i.
 
N

Nick Keighley

so by this time you should know it all :)

(Sam's series), and for nested loops, he writes (p116) "It's often
necessary to create a loop even when you are already in a loop." Then he
goes on to portray a contrived example that doesn't tell me under what
conditions a nested loop might be favoured as a solution? i.e. what are
nested loops useful for? What kinds of algorithms are served by nested
loops? etc. Is any of this making sense? :)

Anyway - thoughts welcomed.

One example you may come across in your book is a basic bubble-sort which
will sort an array of elements, e.g.

for (i=0; i<MAX-1; i++)
for (j=0; j<MAX-1-i; j++)
if (ELEMENT[j+1]>ELEMENT[j])
{
/*swap the elements*/
TEMP = ELEMENT[j+1];
ELEMENT[j+1] = ELEMENT[j];
ELEMENT[j] = TEMP;
}
Thanks Allan. I have heard about bubble sort but have not yet
encountered it. That pleasure yet awaits me!!! :) The code you have
listed here, would this be the standard algorithm for the bubble-sort
method, or are there also other ways of tackling this problem?

note Bubble Sort isn't a very good algorithm (there are faster methods
for
both small and large amounts of data). It is easy to code though.
For real work the standard C library comes with a standard function
qsort().
Unfortunatly qsort() comes with no performance guarantee. It might
even be Bubble sort internally! qsort() is good as a first choice;
replace it if you
know (by measurement) that it is too slow. There are books that
discuss which sort to use when.


--
Nick Keighley

"Beware of bugs in the above code;
I have only proved it correct, not tried it."
-- Donald Knuth
 
M

Morris Dovey

Thanks all. I need to spend more time reading C99, rather than
just looking stuff up from time to time.

Seems like a very strange way to rationalize "anywhere"
declarations/definitions - but perhaps it'll make more sense to
me if I read it enough times...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top