Show that the upper semicontinuous has a maximum 
I found a proof of showing, given that $D$ is compact, if the function $f$ is upper semicontinuous then f achieves a maximum on $D$. But I have a question about the very last sentence: 

and thus $M<\infty$. 

How could this conclusion is obtained, since just before two lines it is said that "It may be that $M=\infty$"? 
Could anyone help me with this? Thanks!

The $M-\frac{1}{n}\leq f(x_n)\leq M$ idea is dubious to me. This already assumes $M$ is finite (proved earlier somewhere?). The idea of the argument still works, though, but we begin with a maximising sequence of $f$, say, $x_n,n\in\mathbb N$. This sequence exists due to how supremum is defined. We have
$$f(x_n)\xrightarrow[n\to\infty]{}\sup _{x\in D} f(x) =:M $$
Due to compactness, we have a convergent subsequence $x_{k_n}\xrightarrow[n\to\infty]{}x_*\in D$ and due to semicontinuity
$$ M = \lim _{n\to\infty} f(x_n) = \limsup_{n\to\infty} f(x_{k_n}) \leq f(x_*) \leq M$$
which immediately excludes the possibility of $M=\infty$ and $f$ attains its supremum (over $D$) at $x_*$.
