so we say that $$\int_{-\pi}^\pi \cos(nx) \cos( mx) dx = \frac{1}{2} [\frac{sin((n+m)x)}{n+m}+ \frac{sin((n-m)x)}{n-m}]_{-\pi}^{\pi} = 0 $$ for $n\not=m$
but if $n =m$ then $$\frac{1}{2} [0+ \frac{sin(2mx)}{n-m}]_{-\pi}^{\pi} = 0$$ $$\frac{2\sin(2m\pi)}{0} = \frac{0}{0}$$
why does one have to assert $n = m$ before integration to avoid this?
Edit: wrong sign, you wrote $2m$ while it was $n-m=0$ in the next to last line.
You skipped a step. Before computing the integral, if $n=m$: $$\int_{-\pi}^\pi\frac{1}{2}\bigl(\cos(2n x)+\cos(0)\bigr)dx$$ The term containing $n-m$ is a constant, so you can't integrate it into a sine function.
It's like when you want to integrate $x^n$, you have to separate the case $n=-1$ before you do the computation, because otherwise you'll step outside the validity domain of a formula.
Edit: another way to view this is to use $\varepsilon=n-m$ and run the computation with $\varepsilon\neq 0$. At the end, do a Taylor expansion of the problematic term to get the correct result.