472,958 Members | 2,143 Online

# 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 7243
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 thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 46 by: Neptune | last post by: Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are... 5 by: dawn | last post by: Hi, Once again I have a question : In my app, I have nested loops, very basic stuff, like for (int i = 0; i < 50; i++) { for (int j = 0 ; j < 75; j++) { 17 by: Peter Olcott | last post by: http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html Why is C# 500% slower than C++ on Nested Loops ??? Will this problem be solved in... 13 by: PeterZ | last post by: Hi, Back to basics! My understanding is that the only way to exit a For-Next loop prematurely is with the 'break' keyword. How are you supposed to do that if you're inside a Switch... 9 by: Javaman59 | last post by: Using local declarations within a block often makes code more readable, but is it less efficient? eg... void P() { while (...) { int i = ...; bool b = ...; .... } } 77 by: Peter Olcott | last post by: http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop.... 2 by: cloc3 | last post by: I'm a newbie in python, and I'm fighting against nested blocks in psp. Thats a little example with a funny behaviour: