$\quad \;$For a one-dimensional harmonic oscillator of mass $\;M\;$ and angular frequency $\;\omega\;$ calculate $\left\langle k \vert \hat{x} \vert n\right\rangle$, where $\left\vert k\right\rangle$ and $\left\vert n\right\rangle$ are eigenstates of the harmonic oscillator, and show that it vanishes unless $k=n\!\pm\!1$.
I am currently doing a question on Harmonic Oscillators, I (sort of) understand the notation until it gets to the part where delta is included. Why is it there?
\begin{equation} \hat{a}^{\dagger}\left\vert n\right\rangle=\sqrt{n\!+\!1}\left\vert n\!+\!1 \right\rangle\:, \qquad \hat{a}\left\vert n\right\rangle=\sqrt{n}\left\vert n\!-\!1 \right\rangle \tag{01} \end{equation}
\begin{align} \left\langle k \vert \hat{x} \vert n\right\rangle & =\sqrt{\dfrac{\hbar}{2m\omega}}\biggl(\left\langle k \left\vert \hat{a}\vphantom{\hat{a}^{\dagger}}\right\vert n \right\rangle +\left\langle k \left\vert \hat{a}^{\dagger} \right\vert n \right\rangle\biggr) \nonumber\\ &=\sqrt{\dfrac{\hbar}{2m\omega}}\Bigl(\sqrt{n}\left\langle k \vert n\!-\!1 \right\rangle +\sqrt{n\!+\!1}\left\langle k \vert n\!+\!1 \right\rangle \Bigr) \nonumber\\ &=\sqrt{\dfrac{\hbar}{2m\omega}}\Bigl(\sqrt{n}\,\delta_{k,n-1} +\sqrt{n\!+\!1}\,\delta_{k,n+1} \Bigr) \tag{02} \\ &= \begin{cases} \sqrt{\dfrac{\hbar n}{2m\omega}} & \: k=n\!-\!1 \vphantom{\sqrt{\dfrac{\hbar n}{2m\omega}}^{\frac12}}\\ \sqrt{\dfrac{\hbar\left(n\!+\!1\right)}{2m\omega}} & \: k=n\!+\!1 \vphantom{\sqrt{\dfrac{\hbar n}{2m\omega}}^{\frac12}}\\ \qquad 0 & \: k \ne n\!\pm\!1 \vphantom{\sqrt{\dfrac{\hbar n}{2m\omega}}^{\frac12}} \end{cases} \nonumber \end{align}