Go through array 4x4 with for, I did not understand

0

I did not understand this code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i, j;
    for(i=1; i<5; i++) {
        for(j=1; j<5; j++) {
            if(i==j)
                printf("1 ");
            else
                printf("0 ");
        }
        printf("\n");
    }
    system("pause");
    return 0;
}

It assigns value 1 if it is true and 0 for false, but if the two are the same, I did not understand, because only one vertical line 1. and not all 1.

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
    
asked by anonymous 20.07.2015 / 01:21

1 answer

4
  • 1st iteration of 1st for variable i is 1 .

  • Once you enter this for you enter a second for (which is within the first) strong> is also 1 will be printed soon.

  • In the next iterations, the j value of the second for will not match that of 1st for > since the i value has not yet been changed because it has not yet completed 1st iteration.

  • Once the 1st iteration is completed in the 1st for value the i value will be incremented for 2 and the value will only hit j when 2nd is in the second iteration, printing 1 . And so on.

  • As soon as both external and internal are in the 1st , 2nd , 3rd and 4th iteration) p>

20.07.2015 / 01:33