<p>Today when I was in computer organization class, teacher talked about something interesting to me. When it comes to talk about Why cache memory works, he said that:</p>

<pre><code>for (i=0; i&lt;M; i++)
   for(j=0; j&lt;N; j++)
      X[i][j] = X[i][j] + K; //X is double(8 bytes)
</code></pre>

<p>it is not good to change the first line with the second. What is your opinions on this? And why it is like that?</p>

It is like that becauses caches like locality. The same number of memory accessed, but spaced further apart, will hit different "lines" of cache, or might even miss the cache altogether. It is therefore good, whenever you have the choice, to organize data so that accesses that are likely to happen close to each other in time, also do so in space. This increases the chance of a cache hit, and gives you more performance.
There is of course a wealth of information about this topic available, see for instance
this wikipedia entry on locality of reference
. Or, I guess, your own course text book. :)