awk with For loop

Hi

My Requirement is to take the sum of each column
below is the input file.

1 2 3 4
1 2 3 4
1 2 3 4

Initial i was using below command to achieve my desired result. however this was adding the row and not column.
i am not able understand why this is happening

 awk '{for(i=1;i<=NF;i++)x+=$i;print x;x=0}' M.txt  ----->1 

After many hit&trial attempt and googling i am able to sum up the column . which is mentioned below.

 awk '{for(i=1;i<=NF;i++) sum+=$i;};END {for (k=1;k<=NF;k++) print sum[k];}' t.txt ------->2

Also not able to understand why using this bracket

 [] is giving correct solution. 

although i have achieved my target but not able to understand the detail working of the command.
as in both the command i am using NF but still in command 1 it sum up the row and not column.

scriptor

awk works in a way that it reads one input record (one line by default), executes the entire script on that line, and the proceeds to the next record.

In command 1, for every single input line you are summing up all fields in that line into a scalar variable, and then print (and reset) that variable.
In command 2, in lieu of using a scalar variable, you are using an array (indicated by the square brackets) indexed by the field No. to sum up the respective fields' values for ALL input lines, and print the sums in the END section.
See the difference now? Good reading is man awk .