So, for example, we have the integral: $$I = \int{\sqrt{1+\sin x}}dx$$
and using the WA method, we solve it like this:

Can you please explain me the last step, why does the solution change due to restricted values? And how do I change it step by step?
Edit: This is the way the teacher did it:
$$I = \int{\sqrt{1+sinx}}dx$$ Since $\sin{x}=2\sin{\frac{x}{2}}\cos{\frac{x}{2}}$ $$I = \int{\sqrt{1+2\sin{\frac{x}{2}}\cos{\frac{x}{2}}}dx}=\int{|\sin{\frac{x}{2}}\cos{\frac{x}{2}}|}dx=\sqrt{2}\int{|\sin(x+\frac{\pi}{4})|dx}=$$ $$=C + \sqrt{2}\int_{-\frac{\pi}{4}}^x{|sin(t+\frac{\pi}{4})|dt=C+\sqrt{2}\int_0^{x+\frac{pi}{4}}{|\sin{z}|dz}}$$ If we say $k=[\frac{x+\frac{\pi}{4}}{\pi}]$ then $\frac{x+\frac{\pi}{4}}{\pi}=k+l$ where $0<=l<1$. That yields $x+\frac{\pi}{4}=k\pi+s$ where $0 <=s <\pi$ Thus, $$I=C+\sqrt{2}\int_0^{k\pi+s}{|\sin{z}|dz}=$$ $$=C+\sqrt{2}\int_0^{k\pi}{|\sin{z}|dz}+\int_{k\pi}^{k\pi+s}{|\sin{z}|dz}=$$ $$=C+2\sqrt{2}+\int_{k\pi}^{k\pi+s}{|\sin{z}|dz}=$$ $$=C+2\sqrt{2}+\int_0^s{\sin{z}dz}=$$ $$=C+2\sqrt{2}+1-\cos{s}=C_1+2\sqrt{2}k-\cos(x+\frac{\pi}{4}+k\pi)=$$ $$=C_1+2\sqrt{2}k-(-1)^k\cos(x+\frac{\pi}{4})$$
I would really appreciate the explanation of what is done here, including the restrictions.