468,107 Members | 1,547 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,107 developers. It's quick & easy.

Nested for loop; curios behaviour?

Please ignore the extreme simplicity of the task :-) I'm new to C,
which explains why I'm doing an exercise like this.

In the following tripple nested loop:

int digit1 = 1;
int digit2 = 0;
int digit3 = 0;
for( ; digit1 < 5 ; digit1++ )
{
for( ; digit2 < 10 ; digit2++ )
{
for( ; digit3 < 10 ; digit3++ )
{
if( digit1 != digit2 && digit2 != digit3 && digit1 != digit3 )
{
printf( "%d%d%d\n", digit1, digit2, digit3 );
}
}
printf("middle loop\n");
}
printf("outer loop\n");
}

I get the following response:

102
103
104
105
106
107
108
109
middle loop
middle loop
middle loop
middle loop
middle loop
middle loop
middle loop
middle loop
middle loop
middle loop
outer loop
outer loop
outer loop
outer loop

This indicates that the program is not running the nested loops more
than once each. However - if I change each for to something like this:

for( digit1 = 0; digit1 < 5 ; digit1++ )

it does an actual tripple nested loop.

I'm guessing that this is part of the ANSI C standard, but I was
wondering why. According to "The C Programming Language" 2nd Edition
by Brian W. Kernighan and Dennis M. Ritchie, all three expressions in
the for(...) statement are optional, but if leaving the first
expression out changes the behaviour of the for(...), then how is it
optional?

Sincerely
Martin Schou
Nov 13 '05 #1
5 6997
In article <20**************************@posting.google.com >,
Martin Schou wrote:
[cut]
int digit1 = 1;
int digit2 = 0;
int digit3 = 0;
for( ; digit1 < 5 ; digit1++ )
{
for( ; digit2 < 10 ; digit2++ )
No re-initialization of digit2, it retains it's value from last
iteration of the outer loop.
{
for( ; digit3 < 10 ; digit3++ )
The same for digit3 here.
{
if( digit1 != digit2 && digit2 != digit3 && digit1 != digit3 )
{
printf( "%d%d%d\n", digit1, digit2, digit3 );
}
}
printf("middle loop\n");
}
printf("outer loop\n");
} [cut] This indicates that the program is not running the nested loops more
than once each. However - if I change each for to something like this:

for( digit1 = 0; digit1 < 5 ; digit1++ )

it does an actual tripple nested loop.

I'm guessing that this is part of the ANSI C standard, but I was
wondering why. According to "The C Programming Language" 2nd Edition
by Brian W. Kernighan and Dennis M. Ritchie, all three expressions in
the for(...) statement are optional, but if leaving the first
expression out changes the behaviour of the for(...), then how is it
optional?


You left the initialization part out of the for() construct,
which means that the loop variable, if one may call it that,
never got re-initialized to zero (or whetever) when entering the
loop again.

A shorter example:

#include <stdio.h>
int main()
{
int i=0, j=0;

for (;i < 10; ++i)
for (;j < 10; ++j)
printf("i=%d, j=%d\n", i, j);

return 0;
}

Output:

i=0, j=0
i=0, j=1
i=0, j=2
i=0, j=3
i=0, j=4
i=0, j=5
i=0, j=6
i=0, j=7
i=0, j=8
i=0, j=9

For i=1, j will already have reached the limit where "j < 10" is
no longer true, which means that the inner loop won't execute at
all.

It does not "change the behaviour of the for() loop", it is just
a consequence of using it in a particular way.
--
Andreas Kähäri
Nov 13 '05 #2
Martin Schou wrote:

[re: for (; test; step) body;]
I'm guessing that this is part of the ANSI C standard, but I was
wondering why. According to "The C Programming Language" 2nd Edition
by Brian W. Kernighan and Dennis M. Ritchie, all three expressions in
the for(...) statement are optional, but if leaving the first
expression out changes the behaviour of the for(...), then how is it
optional?


"Optional" meaning "you don't need to put anything here", not "if you
leave it out, the compiler guesses what the right thing to put in is".

Or, to put it another way, it's required that you have *something*
there, but the something is allowed to be no-thing, in which case,
that's what it does - nothing.

Similarly, the else-part of an if is optional; you don't need to
write one, and if you don't, it is as though you had written one
that doesn't do anything.

Almost similarly, the initialisation part of a variable declaration
is optional, but leaving it out leaves the variable uninitialised,
containing dangerous rubbish.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #3
Chris Dollin wrote:
Almost similarly, the initialisation part of a variable declaration
is optional, but leaving it out leaves the variable uninitialised,
containing dangerous rubbish.

In the case of an automatic variable, yes. For static or file scope
variable, implicit initialization is performed (to the type-appropriate
0 value).

Brian Rodenborn
Nov 13 '05 #4
> No re-initialization of digit2, it retains it's value from last
iteration of the outer loop.


NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO OOOOOOOOOOOOOOOOOOOO!!!!

You IDIOT!!!! Not you - me! GRAH!

Okay, so I'm new to C, but for crying out loud, when you've been
programming for the last five years, you don't [expletive deleted]
make such idiotic mistakes!

ARGH!

Okay, enough venting. Jeez!!!

/-Martin Scou

PS. Sorry for spamming the group with a stupid question.
Nov 13 '05 #5
Martin Schou wrote:
No re-initialization of digit2, it retains it's value from last
iteration of the outer loop.

NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO OOOOOOOOOOOOOOOOOOOO!!!!

You IDIOT!!!! Not you - me! GRAH!

Okay, so I'm new to C, but for crying out loud, when you've been
programming for the last five years, you don't [expletive deleted]
make such idiotic mistakes!

ARGH!

Okay, enough venting. Jeez!!!

/-Martin Scou

PS. Sorry for spamming the group with a stupid question.


I wouldn't get too torn up about it. Mistakes seem a lot more obvious
after the fact.

Matt Gregory

Nov 13 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

46 posts views Thread by Neptune | last post: by
5 posts views Thread by dawn | last post: by
9 posts views Thread by Javaman59 | last post: by
2 posts views Thread by cloc3 | last post: by
3 posts views Thread by Dieter Maurer | last post: by
8 posts views Thread by Nathan Sokalski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.