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