Nested for loop; curios behaviour?

M

Martin Schou

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
 
A

Andreas Kahari

[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.
 
C

Chris Dollin

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.
 
D

Default User

Chris said:
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
 
M

Martin Schou

No re-initialization of digit2, it retains it's value from last
iteration of the outer loop.

NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO!!!!

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.
 
M

Matt Gregory

Martin said:
No re-initialization of digit2, it retains it's value from last
iteration of the outer loop.


NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO!!!!

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
 

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,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top