Velocity Reviews > Use of nested loops.

# Use of nested loops.

Ben Pfaff
Guest
Posts: n/a

 11-06-2003
Morris Dovey <(E-Mail Removed)> writes:

> http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> > pete <(E-Mail Removed)> wrote:
> >
> >>Except that loops don't always have blocks.

> > They do in C99.

>

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.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}

Morris Dovey
Guest
Posts: n/a

 11-06-2003
Alex wrote:

> Morris Dovey <(E-Mail Removed)> wrote:
>
>>(E-Mail Removed) wrote:
>>
>>>pete <(E-Mail Removed)> wrote:
>>>
>>>
>>>>Except that loops don't always have blocks.
>>>
>>>They do in C99.

>
>

>
>
>> #include <stdio.h>
>> int main(void)
>> { int i,j,k;
>> for (i=0; i<10; i++) <- 1

> <- 2
>> for (j=0; j<10; j++) <- 3

> <- 4
>> for (k=0; k<10; k++) <- 5
>> puts("Where's the blocks?"); <- 6
>> return 0;
>> }

>
>
> 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?

--
Morris Dovey
West Des Moines, Iowa USA
Read my lips: The apple doesn't fall very far from the tree.

Ben Pfaff
Guest
Posts: n/a

 11-06-2003
Morris Dovey <(E-Mail Removed)> writes:

> 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).
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x1f6},*p=
b,x,i=24;for(;p+=!*p;*p/=4)switch(x=*p&3)case 0:{return 0;for(p--;i--;i--)case
2:{i++;if(1)break;else default:continue;if(0)case 1utchar(a[i&15]);break;}}}

Alex
Guest
Posts: n/a

 11-06-2003
Morris Dovey <(E-Mail Removed)> wrote:
> Alex wrote:

>> Morris Dovey <(E-Mail Removed)> wrote:
>>
>>>(E-Mail Removed) wrote:
>>>
>>>>pete <(E-Mail Removed)> wrote:
>>>>
>>>>
>>>>>Except that loops don't always have blocks.
>>>>
>>>>They do in C99.

>>
>>

>>
>>
>>> #include <stdio.h>
>>> int main(void)
>>> { int i,j,k;
>>> for (i=0; i<10; i++) <- 1

>> <- 2
>>> for (j=0; j<10; j++) <- 3

>> <- 4
>>> for (k=0; k<10; k++) <- 5
>>> puts("Where's the blocks?"); <- 6
>>> return 0;
>>> }

>>
>>
>> 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?

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

Keith Thompson
Guest
Posts: n/a

 11-06-2003
Morris Dovey <(E-Mail Removed)> writes:
[...]
> 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.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"

Nick Keighley
Guest
Posts: n/a

 11-06-2003
Neptune <neptune@no_spam_here> wrote in message news:<3fa82d7f@212.67.96.135>...
> Allan Bruce wrote:
> > "Neptune" <neptune@no_spam_here> wrote in message
> > news:3fa6a878@212.67.96.135...

> >>I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)"

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

Morris Dovey
Guest
Posts: n/a

 11-08-2003
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...

--
Morris Dovey
West Des Moines, Iowa USA
Read my lips: The apple doesn't fall very far from the tree.