Yes, sometimes it is possible. It depends on what the nested loop actually does of course, but there are cases where a single loop can achieve the same end.
For instance, the following nested loop prints the product of each pair of values in the range 1 to 10:
for( int x=1; x<=10; ++x )
for( int y=1; y<=10; ++y )
printf( "%d * %d = %d\r\n", x, y, x*y );
This could be written as a single loop:
for( int z=0; z<100; ++z )
{
int x = z/10+1;
int y = z%10+1;
printf( "%d * %d = %d\r\n", x, y, x*y );
}
Note that the nested loop is easier to read and maintain and is also much more efficient than the merged loop.
'Standard' for statement:
for (exp1; exp2; exp3) statement
'Nested' for statement:
for (exp1; exp2; exp3) statement
You may exit a nested loop in Java using a break with a label for the outer loop.
If one loop ends before the next begins then they are not nested at all -- they are completely independent. To be nested, one loop must contain the other loop in its entirety. That is, the inner, nested loop must start and end within the outer, containing loop. Nested loop example (in C++): for( int x = 0; x < 10; ++x ) // outer loop { for( int y = 0; y < 10; ++y ) // inner loop (nested loop) { printf( "%d x %d = %d\r\n", x, y, x*y ); } // end of inner loop } // end of outer loop
The nested loop.
Sometimes you have to use nested loops, in this case one of them is the outer, the other is the inner.
Nested loop, you mean; one loop in the other loop, eg: for (i=0; i<10; ++i) { for (j=0; j<i; ++j) { printf ("i=%d, j=%d\n", i, j); } }
You may exit a nested loop in Java using a break with a label for the outer loop.
If one loop ends before the next begins then they are not nested at all -- they are completely independent. To be nested, one loop must contain the other loop in its entirety. That is, the inner, nested loop must start and end within the outer, containing loop. Nested loop example (in C++): for( int x = 0; x < 10; ++x ) // outer loop { for( int y = 0; y < 10; ++y ) // inner loop (nested loop) { printf( "%d x %d = %d\r\n", x, y, x*y ); } // end of inner loop } // end of outer loop
The nested loop.
A nested loop is a (inner) loop that appears in the loop body of another (outer) loop. The inner or outer loop can be any type: while, do while, or for. For example, the inner loop can be a while loop while an outer loop can be a for loop.
Please ask clearly what you want to do with the image and explain why a nested for-loop is necessary.
In C a structure within a structure is called nested. For example, you can embed a while loop in another while loop or for loop in a for loop or an if statement in another if statement.
Sometimes you have to use nested loops, in this case one of them is the outer, the other is the inner.
A loop inside a loop, which is known as a nested loop.
Nested loop, you mean; one loop in the other loop, eg: for (i=0; i<10; ++i) { for (j=0; j<i; ++j) { printf ("i=%d, j=%d\n", i, j); } }
Nested loops can be used in any language. They are used for situations where you may need two levels of repetition. So you could be printing a list of teams, which is one loop, and for each team the name of its players, and that would be the inner loop. If you know there is a set amount of players and teams, a For loop would be appropriate. You could have a loop that displays the 7 days of the week and for each day, the on the hour times, so that would require two For loops with the hours one nested in the days one. There are all sorts of situations where you would use them.
Concentric Circles
Easy. Change any + to -, any += to -=, any ++ to --