algorithms, number of instructions in the code
The factors of $\tfrac12$ come from the fact that $1 + \dots + N = \tfrac12N(N+1)$.

j is incremented at the end of the loop. This is standard syntax for languages such as C and Java.