How to guess the value of $j$ at the end of the loop?
If you unfold the loop you get:

$$\text{val}( j)=n+n/2+n/4+ n/8 \ldots$$

In total you have $\log n$ terms.
See this post, how to evaluate the sum.