473,396 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 7269
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.