what is formula of nested loop?

All 11 Replies

There is no formula, unless you mean syntax.

I think it will be better for you if you read a book on C. For example, you can read The C Programming Language by K & R. There are other books also. These books have code examples for you to understand better.

Nesting a loop inside another loop is very common.

Suppose you have a matrix of integers.

Suppose the matrix has 3 rows and 4 integers in each row ...

Now if you wanted to print out that matrix, a common C, simple way, would be to code like this:

code for above post :)

``````int matrix[3][4] = {{3, 4, 5, 6},
{1, 4, 6, 9},
{8, 3, 5, 2}}
``````

Printing the array using nested for loops:

``````for(int i = 0; i < 3; i++){ //initialize the for loop.  for(counter var; exit condition; incriment the var)
for(int j = 0; j < 4; j++){
printf("%d ", matrix[i][j]); //print the number at position i,j in the matrix
}
printf("\n");  //print a new line
}
``````

Using while loops:

``````int i = 0;  //initialize the counter vars
int j = 0;
while(i < 3){  //start the while loop. while(condition is true)
while(j < 4){
printf("%d ", matrix[i][j]);  //print the number at position i,j in the matrix
j++;        //increment the counter so we dont get an infinite loop.
}
printf("\n");
i++;
}
``````

Both code snippits would print:

``````3 4 5 6
1 4 6 9
8 3 5 2
``````

Hey

"DarkLightning7
Junior Poster " ,

I am really new here ... could someone please explain what happened ... that my example code was deleted and hi-jacked?

Is this the way things are supposed to work here?

Your code was not deleted (have just checked the editing history, no moderator deleted anything) - perhaps your edit fell outside the allowed time window for such things by the time you had finished and so wasn't actually applied?

Ok ... thanks ... that's good to hear and know :)

And ... sorry ... for jumping too quickly to conclusion.

And ... thanks to poster ... for filling in the code missing ...

Looks like my late edit didn't show any code posted ... just a ':'

Actually the C coding standard often used does not allow declaring variables inside a 'for loop' ...

So, in C, you could code like this:

``````/*
This is an example of a function that you could call to print out a matrix
Note that here, ROWS and COLS are presummed to be GLOBAL CONSTANTS
in C you would use (for example) ...
#define ROWS 3
#define COLS 4
Note: ROWS and COLS must be 'defined' in your program BEFORE being called in your program
*/

void printMatrix( int matrix[ROWS][COLS] )
{
int i;
for( i = 0; i < ROWS; ++ i )
{
int j;
for( j = 0; j < COLS; ++ j )
{
printf( "%d ", matrix[i][j] );
}
putchar( '\n' ); /* print a new line ... */
}
}
``````

Actually the C coding standard often used does not allow declaring variables inside a 'for loop' ...

It depends on which standard you're compiling for. Prior to C99 you cannot declare a variable in the initialization clause of a for loop. Starting with C99, you can.

Note that prior to C99 you might be able to do it, but your code would not be portable. This is usually the case when the compiler supports this feature as an extension, or if the code is being compiled as C++.

first inner loop execute then outer loop execute when inner loop condition false then return outer loop.
when outer loop condtion false

You can also traverse the array linearly,

``````int height(13), width(7);
int matrix[height][width];
for(int i=0;i<height*width;++i)
{
if( !(i%width) ) printf( "\r\n" );
printf( "%d ", matrix[ i / width ][ i % width ] );
}
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.