472,958 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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: <html> <form> <select name="List"> <!--makes a list with a loop --> <%...
3
by: Dieter Maurer | last post by:
I met the following surprising behaviour .... for i in range(3): .... def gen1(): .... yield i .... yield i, gen1() .... .... 0 0 1 1
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.